Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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:25am
Edited 15-Jun-12 10:40am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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 at 15-Jun-12 20:24pm
   
Correct +5!
Clifford Nelson at 15-Jun-12 22:37pm
   
Thanks
SAKryukov at 15-Jun-12 21:41pm
   
5ed,
--SA
Clifford Nelson at 15-Jun-12 22:37pm
   
Thanks
Manas Bhardwaj at 21-Jun-12 3:16am
   
correct +5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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 at 15-Jun-12 20:24pm
   
Correct +5!
VJ Reddy at 15-Jun-12 20:32pm
   
Thank you, Manas :)
SAKryukov at 15-Jun-12 21:41pm
   
Yes, a 5.
--SA
VJ Reddy at 15-Jun-12 22:38pm
   
Thank you, SA :)
taha bahrami at 16-Jun-12 3:56am
   
very good!
5!
VJ Reddy at 16-Jun-12 4:06am
   
Thank you, taha :)
losmac at 20-Jun-12 10:23am
   
Good answer, my 5!
VJ Reddy at 20-Jun-12 12:19pm
   
Thank you, losmac :)
Sandeep Mewara at 21-Jun-12 16:46pm
   
Correct 5!
VJ Reddy at 21-Jun-12 19:42pm
   
Thank you, Sandeep :)
Espen Harlinn at 23-Jun-12 4:07am
   
5'ed!
VJ Reddy at 23-Jun-12 8:09am
   
Thank you, Espen :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 at 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)

  Print Answers RSS
0 OriginalGriff 435
1 Sergey Alexandrovich Kryukov 200
2 Yogesh Kumar Tyagi 180
3 Richard Deeming 180
4 RyanDev 115
0 Sergey Alexandrovich Kryukov 6,405
1 OriginalGriff 5,798
2 CPallini 2,473
3 Richard MacCutchan 1,647
4 Abhinav S 1,530


Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 15 Jun 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