-
August 23rd, 2011, 07:55 PM
#1
Using Regex to get text between delimeters
Hello,
I'm trying to parse a string and I'm having trouble getting arguments from it. The string may appear as follows:
"Banannas(1) garbage Apples(2) iheartcodeguru"
where each substring can appear in any order; it could just as well be "garbage iheartcodeguru Apples(2) Banannas(1)".
I'l like to get each argument one at a time. For example, for the Banannas argument, I wrote the following code:
Code:
string q = "Banannas(1) garbage Apples(2) iheartcodeguru";
Regex Pos = new Regex(@"Banannas(([A-Za-z0-9\-]+))");
Match PosMatch = Pos.Match(q);
string tempval = PosMatch.Groups[1].Value;
Console.WriteLine(tempval);
I expect an output of "1", but tempval always ends up as "". What am I doing wrong?
-
August 24th, 2011, 12:21 AM
#2
Re: Using Regex to get text between delimeters
Since parenthesis delimit the parameters you are trying to extract, you'll need to escape them when you mean to match a parenthesis literally. Thus, change your regex to:
Code:
Regex Pos = new Regex(@"Banannas\(([A-Za-z0-9\-]+)\)");
This worked for me when I ran your snippet.
Hope that helps!
Best Regards,
BioPhysEngr
http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
-
August 27th, 2011, 08:27 PM
#3
Re: Using Regex to get text between delimeters
Sorry to bring this thread up back from the dead... I was unable to rep you so I just wanted to say thanks again for your help. That's twice in a row now that you've helped me out.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|