Click here to Skip to main content
15,030,908 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need help writing a regex that will get the value out off the following string...

I need to try to get the number 002 out of this text string. SysParaI is the identifier. For example when I see "SysParaI" I need to grab the number in the following paren (002)?

STRING[261] Directory(\FTP\OSP_003\)
STRING[9] FileName(SysParaI)
STRING[4] FileExtension(002)
Posted
Updated 15-Jun-12 10:40am
v3

You can go simple and just

C#
Regex r = new Regex(@"\(.*\)");
var e = r.Matches(s);


e is a collection of matches, and will include the "()"

Will not requer

C#
Regex r = new Regex(@"\((.*)\)");
var e = r.Match(s);


e.Groups will contain all matches, the second item will your result
   
Comments
Manas Bhardwaj 15-Jun-12 20:24pm
   
Correct +5!
Clifford Nelson 15-Jun-12 22:37pm
   
Thanks
Sergey Alexandrovich Kryukov 15-Jun-12 21:41pm
   
5ed,
--SA
Clifford Nelson 15-Jun-12 22:37pm
   
Thanks
Manas Bhardwaj 21-Jun-12 3:16am
   
correct +5
I think the matching with prefix and suffix options of Regex can be used to extract only the required value from the parentheses as shown below:
C#
string text = @"STRING[261] Directory(\FTP\OSP_003\)\nSTRING[9] FileName(SysParaI)\nSTRING[4] FileExtension(002)";
Match match = Regex.Match(text, @"(?<=\(SysParaI\)[^)(]*\()\d+(?=\))",
                 RegexOptions.CultureInvariant);
if (match.Success)
	Console.WriteLine (match.Value);
//Output
//002

It can be tested here http://regexhero.net/tester/[^]

The above pattern does not match if there are spaces around 002 and/or SysParaI like ( SysParaI ) ( 002 ).

To match in such case the following pattern can be used.
(?<=\(\s*SysParaI\s*\)[^)(]*\(\s*)\d+(?=\s*\)
   
Comments
Manas Bhardwaj 15-Jun-12 20:24pm
   
Correct +5!
VJ Reddy 15-Jun-12 20:32pm
   
Thank you, Manas :)
Sergey Alexandrovich Kryukov 15-Jun-12 21:41pm
   
Yes, a 5.
--SA
VJ Reddy 15-Jun-12 22:38pm
   
Thank you, SA :)
   
very good!
5!
VJ Reddy 16-Jun-12 4:06am
   
Thank you, taha :)
Maciej Los 20-Jun-12 10:23am
   
Good answer, my 5!
VJ Reddy 20-Jun-12 12:19pm
   
Thank you, losmac :)
Sandeep Mewara 21-Jun-12 16:46pm
   
Correct 5!
VJ Reddy 21-Jun-12 19:42pm
   
Thank you, Sandeep :)
Espen Harlinn 23-Jun-12 4:07am
   
5'ed!
VJ Reddy 23-Jun-12 8:09am
   
Thank you, Espen :)
If this is a single string, I would think "SysParaI\).*?\((\d+)\)" should work, if you set RegexOptions.Multiline (see more about that here[^])

If each line is it's own string, check the previous one for the presence of SysParaI then use "\((\d+)\)
   
Comments
Member 9014541 15-Jun-12 16:59pm
   
No... its a single long string from a flat txt file and there is only one SysPara in the whole file so this should work... THANKS!!!!

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