Click here to Skip to main content
Sign Up to vote bad
good
See more: C#RegEx
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 15 Jun '12 - 10:25
Edited 15 Jun '12 - 10:40


3 solutions

You can go simple and just
 
Regex r = new Regex(@"\(.*\)");
var e = r.Matches(s);
 
e is a collection of matches, and will include the "()"
 
Will not requer
 
Regex r = new Regex(@"\((.*)\)");
var e = r.Match(s);
 
e.Groups will contain all matches, the second item will your result
  Permalink  
Comments
Manas Bhardwaj - 15 Jun '12 - 20:24
Correct +5!
Clifford Nelson - 15 Jun '12 - 22:37
Thanks
SAKryukov - 15 Jun '12 - 21:41
5ed, --SA
Clifford Nelson - 15 Jun '12 - 22:37
Thanks
Manas Bhardwaj - 21 Jun '12 - 3:16
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:
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*\)
  Permalink  
Comments
Manas Bhardwaj - 15 Jun '12 - 20:24
Correct +5!
VJ Reddy - 15 Jun '12 - 20:32
Thank you, Manas :)
SAKryukov - 15 Jun '12 - 21:41
Yes, a 5. --SA
VJ Reddy - 15 Jun '12 - 22:38
Thank you, SA :)
taha bahrami - 16 Jun '12 - 3:56
very good! 5!
VJ Reddy - 16 Jun '12 - 4:06
Thank you, taha :)
losmac - 20 Jun '12 - 10:23
Good answer, my 5!
VJ Reddy - 20 Jun '12 - 12:19
Thank you, losmac :)
Sandeep Mewara - 21 Jun '12 - 16:46
Correct 5!
VJ Reddy - 21 Jun '12 - 19:42
Thank you, Sandeep :)
Espen Harlinn - 23 Jun '12 - 4:07
5'ed!
VJ Reddy - 23 Jun '12 - 8:09
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+)\)
  Permalink  
Comments
Member 9014541 - 15 Jun '12 - 16:59
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)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 425
1 OriginalGriff 315
2 Slacker007 240
3 Aarti Meswania 210
4 Maciej Los 200
0 Sergey Alexandrovich Kryukov 8,893
1 OriginalGriff 7,134
2 CPallini 3,678
3 Rohan Leuva 3,036
4 Maciej Los 2,428


Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 15 Jun 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid