![]() |
| | #1 |
| Registered User Join Date: Jan 2008
Posts: 153
| Regular expressions question 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!!!");
thanks in advance |
| Devils Child is offline | |
| | #2 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| "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" Code: "${1}"
|
| tabstop is offline | |
| | #3 |
| Registered User Join Date: May 2003
Posts: 1,185
| 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;
}
Regular Expression Tutorial - Learn How to Use Regular Expressions
__________________ 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. Last edited by Cat; 08-21-2009 at 08:39 AM. |
| Cat is offline | |
| | #4 |
| Registered User Join Date: Jan 2008
Posts: 153
| works like a charm. thanks for the quick reply |
| Devils Child is offline | |
| | #5 |
| Registered User Join Date: Jan 2008
Posts: 153
| wait a minute: what's with this: "testXXXblahYYYblah" i should get: "XXX" but i get "XXXblahYYY" ??? |
| Devils Child is offline | |
| | #6 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| |
| tabstop is offline | |
| | #7 | |
| Registered User Join Date: May 2003
Posts: 1,185
| Quote:
@"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. | |
| Cat is offline | |
| | #8 |
| Registered User Join Date: Jan 2008
Posts: 153
| works! thanks a lot |
| Devils Child is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Question... | TechWins | A Brief History of Cprogramming.com | 16 | 07-28-2003 09:47 PM |
| Regular Expressions | stormbringer | Windows Programming | 3 | 03-27-2003 09:35 AM |
| opengl DC question | SAMSAM | Game Programming | 6 | 02-26-2003 09:22 PM |
| Regular Expressions again | Inquirer | C++ Programming | 2 | 04-19-2002 10:03 PM |
| Regular Expression Troubles | Unregistered | C++ Programming | 2 | 04-11-2002 04:21 PM |