Regular expressions question

This is a discussion on Regular expressions question within the C# Programming forums, part of the General Programming Boards category; hi! Is it somehow possible to get use the regex "test*blah" and filter what's * out from the text? Code: ...

  1. #1
    Registered User Devils Child's Avatar
    Join Date
    Jan 2008
    Posts
    244

    Regular expressions question

    hi!
    Is it somehow possible to get use the regex "test*blah" and filter what's * out from the text?

    Code:
    			Regex regex = new Regex("test*blah");
    			Match match = regex.Match("YYYtestXXXblahZZZ");
    			if (match.Value == "XXX") MessageBox.Show("That's what I want!!!");
    i suppose it must look like this, but i don't quite get what i want...

    thanks in advance

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    "Normally" you would use a regex like "test(.*)blah" and then use the backreference \1 to refer to the first parenthesized sub-expression (which in this case would appear to be what you want). The C# syntax for backreferences looks a little weird; if I'm reading it right, it would be something like
    Code:
    "test(?<1>.*)blah"
    and then
    Code:
    "${1}"
    in your Result would be what you want. (Of course, with this syntax you can give the thing a meaningful name, instead of "1".)

  3. #3
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Here's the exact code you should need, including a named parameter:

    Code:
    // ExplicitCapture isn't strictly necessary, it makes sure no unnamed groups are captured.  
    // If you only use named groups, turning ExplicitCapture on will improve speed and reduce memory usage.
    Regex ex = new Regex(@"test(?'YourNamedParameter'.*)blah", RegexOptions.ExplicitCapture);
    
    // This also works:
    // Regex ex = new Regex(@"test(?<YourNamedParameter>.*)blah", RegexOptions.ExplicitCapture);
    
    string input = "YYYtestXXXblahZZZ"
    
    Match m = ex.Match(input);
    if (m != null)
    {
        string value = m.Groups["YourNamedParameter"].Value;
    }
    Here's a tutorial on how to make and use regular expressions, this is practically a regex bible:
    Regular Expression Tutorial - Learn How to Use Regular Expressions
    Last edited by Cat; 08-21-2009 at 08:39 AM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  4. #4
    Registered User Devils Child's Avatar
    Join Date
    Jan 2008
    Posts
    244
    works like a charm. thanks for the quick reply

  5. #5
    Registered User Devils Child's Avatar
    Join Date
    Jan 2008
    Posts
    244
    wait a minute: what's with this:
    "testXXXblahYYYblah"

    i should get: "XXX"
    but i get "XXXblahYYY"

    ???

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Devils Child View Post
    wait a minute: what's with this:
    "testXXXblahYYYblah"

    i should get: "XXX"
    but i get "XXXblahYYY"

    ???
    Because that's what matches. Regex is greedy, and will always take the largest substring that fits the pattern.

  7. #7
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by tabstop View Post
    Because that's what matches. Regex is greedy, and will always take the largest substring that fits the pattern.
    That's also covered in that regex tutorial I linked (in the chapter on repetition), but the non-greedy way would be to do this:

    @"test(?'YourNamedParameter'.*?)blah"
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  8. #8
    Registered User Devils Child's Avatar
    Join Date
    Jan 2008
    Posts
    244
    works! thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  2. Regular Expressions
    By stormbringer in forum Windows Programming
    Replies: 3
    Last Post: 03-27-2003, 08:35 AM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  4. Regular Expressions again
    By Inquirer in forum C++ Programming
    Replies: 2
    Last Post: 04-19-2002, 10:03 PM
  5. Regular Expression Troubles
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-11-2002, 04:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21