Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Csharp.net
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 30-Oct-12 1:17am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
I have modified the code a bit and its working
 
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
  Permalink  
v3
Comments
Member 9326300 at 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.
Milind Thakkar at 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
Member 9326300 at 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
Milind Thakkar at 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.
Member 9326300 at 30-Oct-12 14:50pm
   
Dear Milind Thakkar,
you're right, it works by changing the regex as you suggest.
Many thanks.
 
Wajim
Milind Thakkar at 31-Oct-12 0:39am
   
Glad it helped. Do mark it as answer/upvote. - Milind

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



Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 30 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100