Click here to Skip to main content
15,867,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Need help on another REGEX. I have a flat txt string and need to pull a suffix off of files names.

For example, When I see 'SysParaI' I want to grab '002.' Etc.

So far I have, but somethings not right. Anyhelp????

C#
Match match = Regex.Match(TextString,  @"(?<=\(SysPara\)[^)(]*\d+(?=\))"

05/25/12  06:34p                    79 SysParaI.002
05/25/12  06:34p                   437 EqtParaI.016
05/25/12  06:34p                  3143 EqtProgI.022
Posted
Updated 20-Jun-12 5:29am
v2

Note: These suggestions aren't Regex specifically, which I know you asked for. I'll work on a RegEx for you as well, but these solutions seem simpler and lighter than full RegEx:

Pass the file names into the GetExtension method like so:
C#
string extension = System.IO.Path.GetExtension(filename);

This will give you just the "002" or whateve the extension is, as long as you can pass in the file name (and not all of the extra "stuff" in the above lines.

If you can't isolate just the file name, why don't you just grab the last three characters of each line? That would get you the extension quite easily (unless the line doesn't end there).

To use RegEx to find these values, you would need to do something like this:

C#
Match match = Regex.Match("05/25/12 06:34p 79 SysParaI.002", @"SysParaI.([0-9]{3})");
int fileNumber = Convert.ToInt32(match.Groups[1].Value);


This would find the three digits after the decimal where the file started with "SysParaI". It would find matches anywhere in your strings. I would only use this if you were looking for files inside a lot of text and couldn't predict where they would be. You would have to modify the RegEx to accomodate each file name (EqtParaI, EqtProgI, etc.) but this should be easy.
 
Share this answer
 
v3
Comments
Member 9014541 20-Jun-12 11:12am    
Um you know I didn't think of it this way. Doesn't this assume file system access? What if I don't have access to the file system? I mean, I got this string from a telnet session and not file IO....
Tim Corey 20-Jun-12 11:16am    
It shouldn't need file system access. It is just a method that takes a file name in and spits out the extension (it includes the dot so you will need to trim that). I verified it works. I even passed your whole string in (05/25/12 06:34p 79 SysParaI.002) and it still worked.
Member 9014541 20-Jun-12 11:20am    
Yea... I agree... good point.
Tim Corey 20-Jun-12 11:29am    
I added the RegEx, in case you really needed that.
BillW33 20-Jun-12 16:29pm    
Good answer, +5 :)
If you have path and you want to get filenames use the below code, never use regular expressions:

string ext = Path.GetExtension(path);
 
Share this answer
 
Comments
[no name] 22-Jun-12 21:12pm    
How does repeating solution1 answer help answering the question?
[no name] 24-Jun-12 5:51am    
He has updated i guess

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