CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2010
    Location
    .Net 4.0
    Posts
    58

    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?

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    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.

  3. #3
    Join Date
    May 2010
    Location
    .Net 4.0
    Posts
    58

    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
  •  





Click Here to Expand Forum to Full Width

Featured