Click here to Skip to main content
12,702,969 members (31,055 online)
Rate this:
 
Please Sign up or sign in to vote.
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 11:25am
Updated 15-Jun-12 11: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 15-Jun-12 20:24pm
   
Correct +5!
Clifford Nelson 15-Jun-12 22:37pm
   
Thanks
SAKryukov 15-Jun-12 21:41pm
   
5ed,
--SA
Clifford Nelson 15-Jun-12 22:37pm
   
Thanks
Manas Bhardwaj 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 15-Jun-12 20:24pm
   
Correct +5!
VJ Reddy 15-Jun-12 20:32pm
   
Thank you, Manas :)
SAKryukov 15-Jun-12 21:41pm
   
Yes, a 5.
--SA
VJ Reddy 15-Jun-12 22:38pm
   
Thank you, SA :)
taha bahrami 16-Jun-12 3:56am
   
very good!
5!
VJ Reddy 16-Jun-12 4:06am
   
Thank you, taha :)
losmac 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 :)
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 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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 15 Jun 2012
Copyright © CodeProject, 1999-2017
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