Nick
Nick

Reputation: 19664

.Net Regex doesn't match when other tools do

I am trying to match on a string that begins with ./media

I have the following Regular expression:

bool match = Regex.IsMatch(@"^\./media", imgSourcePath);

My source string looks like: ./media/somefile.png

When I test this expression in other tools it works as expected. However a match is never found in C# source implementation. Can someone tell me why?

Thanks!

Upvotes: 0

Views: 720

Answers (2)

GrayFox374
GrayFox374

Reputation: 1782

Your parameters appear to be backwards: Regex.IsMatch(String input, String pattern). Try this:

var m = Regex.IsMatch("./media", "(./media)(.*?)", RegexOptions.IgnoreCase | 
    RegexOptions.Singleline);
MessageBox.Show(m.ToString());  //displays true

Upvotes: 2

Tawnos
Tawnos

Reputation: 1887

http://msdn.microsoft.com/en-us/library/sdx2bds0.aspx#Y30

You have the input and pattern backwards. The string you want to search for a match should be the first parameter, and the pattern to use should be the second.

Upvotes: 1

Related Questions