Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can someone help me with Regex in csharp...
What I"m trying to do is displaying all lines containing a pattern from a text file,
after reading a lot of tuto and using tools like Regex Tester etc... nothing is working.
- File_Info.txt is the target file containing lines with word "01234567_TRYN_YZ"
- example of line to be displayed: 05/23/12 19:31:20: dsetup32\01234567_TRYN_YZ.txt stop to do...
Thanks in advance.

This is my code:

string[] lines = File.ReadAllLines("File_Info.txt");
Regex regex = new Regex("^[0-9]+.[T]\w{3}.[Y]\w{1}$");
foreach (string line in lines)
{
Match match = regex.Match(line.Trim())
if (match.Successful)
{
Console.WriteLine(Line);
}
}
Posted

1 solution

Hi,

I have modified the code a bit and its working

C#
string[] lines = File.ReadAllLines("d:\\milind.txt");
Regex regex = new Regex(@"\d{8}_TRYN_YZ");
foreach (string line in lines)
{
    Match match = regex.Match(line.Trim());
    if (match.Success)
    {
        Console.WriteLine(line);
    }
}


I have used following data in the file.

05/23/12 19:31:21: dsetup32\01234567_TRYN_YZ.txt
05/23/12 19:31:22: dsetup32\_TRYN_YZ.txt
05/23/12 19:31:23: dsetup32\01234567__YZ.txt
05/23/12 19:31:24: dsetup32\01234533_TRYN_YZ.txt
05/23/12 19:31:25: dsetup32\01234544_TRYN_YZ.txt


And it displayed result correctly as

05/23/12 19:31:21: dsetup32\01234567_TRYN_YZ.txt
05/23/12 19:31:24: dsetup32\01234533_TRYN_YZ.txt
05/23/12 19:31:25: dsetup32\01234544_TRYN_YZ.txt


Hope that helps.

Milind
 
Share this answer
 
v3
Comments
[no name] 30-Oct-12 7:09am    
Dear Milind Thakkar,
thank you so much for your prompt reply.
I have tried your modified code and it is working. However the code displays only one line : 05/23/12 19:31:20: dsetup32\01234567_TRYN_YZ.txt stop to do...
Appuyez sur une touche pour continuer...
The line containing "01234567", what about the others with others digits?
Thanks again.
MT_ 30-Oct-12 7:14am    
Hi, Yes, the code is suppose to find entries where it finds exact match "01234567_TRYN_YZ". From your response, I understand, you have a pattern that you are looking. Can you describe the pattern? like
1. First 8 numberic
2. Then underscore (_)
3. Then three alphabet (or exact word TRN)
and so on.
Then I /someone will be able to give correct regex pattern.
Milind
[no name] 30-Oct-12 12:12pm    
Dear,
the pattern match I'm looking for is "\01234567_TRYN_YZ"
where only numbers after the backslash change.
the word "_TRYN_YZ" does not change.

Thank you very much.
Wajim
MT_ 30-Oct-12 13:21pm    
I think a minor change in the regex will do the trick. Use Regex regex = new Regex(@"\d{8}_TRYN_YZ"); Also updated the solution.
[no name] 30-Oct-12 14:50pm    
Dear Milind Thakkar,
you're right, it works by changing the regex as you suggest.
Many thanks.

Wajim

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900