Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex ! 3

Status
Not open for further replies.

zackiv31

Programmer
May 25, 2006
148
US
Why doesn't this work:

Code:
string test = @"multi(0)";
if (Regex.IsMatch(test, test))
     MessageBox.Show("this doesn't print");

In a larger context, I'm extract a line similar to string test, and then checking subsequent lines for it, but because it captures parenthesis, it doesn't match.
 
you would think that would work but i am not that good with them? you could use
Code:
string test = @"multi(0)";
string patt = @"[multi(0)]*";
System.Text.RegularExpressions.Regex.IsMatch( test, patt ) )

Age is a consequence of experience
 
I don't think litton1's solution is what you are after as it will match anything with the letter m in it.

The problem you are having is that the string you are using has characters in it that are reserved in Regular Expressions. The reserved characters are . $ ^ { [ ( | ) * + ? \

To include a reserved character in a regular expression you preceed it with a \ so the sample sting you give becomes @"multi\(0\)"

The following will give you a match:

Code:
string test = @"multi(0)";
            string patt = @"multi\(0\)";
            if (Regex.IsMatch(test, patt))
                Console.WriteLine("Matched");

By the way if all you want to do is see if a string contains the exact source string (without wild cards), you are better off using String.IndexOf(string) as this is much faster. For example:

Code:
string test = @"multi(0)";
if (test.IndexOf(test) >= 0)
     MessageBox.Show("Match");
[CODE]
 
Hey Aptitude, was it your website I downloaded a regex pattern tester/editor from? If so, maybe you should plug it a little bit, it is a handy little tool. Saves me a ton of time every time I need to get a complicated pattern right (much better than my old way of testing using vbscript!) Maybe zackiv31 would find it helpful :)

If not, I'll find the website when I get to work and post a link, but I could've sworn it was your site.

[small]----signature below----[/small]
I don't do any programming whatsoever

Ignorance of certain subjects is a great part of wisdom
 
AlexCurse,

It may well have been my site, if so I'm glad you liked it. The link is it is at the bottom.

I was going to highlight it more once I figured out how to add syntax higlighting, though it is baffleing me how to do this without writing my own edit box control.

Aptitude (AKA Martin)
 
I think the link to your regex tool makes this page very helpful. have a star!

Age is a consequence of experience
 
Star from me too :)

[small]----signature below----[/small]
I don't do any programming whatsoever

Ignorance of certain subjects is a great part of wisdom
 
Chrissie - that was supposed to be our secret!

[small]----signature below----[/small]
I don't do any programming whatsoever

Ignorance of certain subjects is a great part of wisdom
 
How is indexOf() faster? wouldn't an IsMatch() be the quickest? (just thinking logically)

and I fixed it by doing a Regex.Escape(myLine)... to escape those lines that need escaping.
 
String.IndexOf(searchString) is faster because you don't need to create a RegEx object at all. Since you are only looking for the literal string "multi(0)", there is really no need to use regex.

Hope this helps,

Alex

[small]----signature below----[/small]
I don't do any programming whatsoever

Ignorance of certain subjects is a great part of wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top