I basically want a regex expression that only matches if the string doesn't end in ".php".
The only thing I know of would be "[^.php]". But because of how [^x] works, it would also match things ending in "php.", etc.
Printable View
I basically want a regex expression that only matches if the string doesn't end in ".php".
The only thing I know of would be "[^.php]". But because of how [^x] works, it would also match things ending in "php.", etc.
I think the regex you want is: \.php$ and then just do a not of the IsMatch()
Hey,
Where are you getting the string from? Are you looping through a set of files on the file system? If so, why not use the methods on the Path Class:
http://msdn.microsoft.com/en-us/libr...h_methods.aspx
Such as, GetExtension:
http://msdn.microsoft.com/en-us/libr...extension.aspx
Just thought I would put it out there as an alternative.
Hope that helps!!
Gary
Both options are very workable :) To use "pure" regex, a negative look-behind assertion would be what you're after:
Code:Regex t = new Regex(@".*$(?<!\.php)");
Console.WriteLine(t.IsMatch("myfile.txt") + " *" + t.Match("myfile.txt").Value + "*");
Console.WriteLine(t.IsMatch("myfile.php") + " *" + t.Match("myfile.php").Value + "*");
Console.WriteLine(t.IsMatch("myfile.php.txt") + " *" + t.Match("myfile.php.txt").Value + "*");