C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-21-2009, 07:28 AM   #1
Registered User
 
Join Date: Jan 2008
Posts: 153
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
Devils Child is offline   Reply With Quote
Old 08-21-2009, 07:55 AM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
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"
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".)
tabstop is offline   Reply With Quote
Old 08-21-2009, 08:35 AM   #3
Cat
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;
}
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
__________________
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   Reply With Quote
Old 08-21-2009, 09:13 AM   #4
Registered User
 
Join Date: Jan 2008
Posts: 153
works like a charm. thanks for the quick reply
Devils Child is offline   Reply With Quote
Old 08-21-2009, 09:35 AM   #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   Reply With Quote
Old 08-21-2009, 09:44 AM   #6
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 08-21-2009, 10:07 AM   #7
Cat
Registered User
 
Join Date: May 2003
Posts: 1,185
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.
Cat is offline   Reply With Quote
Old 08-21-2009, 01:18 PM   #8
Registered User
 
Join Date: Jan 2008
Posts: 153
works! thanks a lot
Devils Child is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 03:36 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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