Groups | Blog | Home
all groups > c# > may 2004 >

c# : Regex confusion


Tom Jones
5/8/2004 7:24:40 PM
Hi,

I have a component that accepts a string representing a class of files
(exactly like those you would pass to the 'dir' dos command, ie. '*.txt', or
'???.cpp').

An exception is generated if I try to create a Regex like this:

Regex r = new Regex("*.txt");

I understand why that is a problem, thus I parse my filter strings as such:
'*' gets replaced by '.*', '?' gets replaced by '.{1}', and any '.' found in
the original filter is replaced with its hex representation, '\x2E'.

Thus something like "???.a" (any 3 letter filename that ends in an 'a') we
be sent to the Regex ctor as such:

Regex r = new Regex(@"^.{1}.{1}.{1}\x2Ea$");

My problem is that the result of r.IsMatch("0.0.1235.a") is *true*!!??

Can someone please explain why r.IsMatch() returns true in this case?

Thanks!
Tom

Tom Jones
5/8/2004 7:50:14 PM

[quoted text, click to view]

Sorry about that - it *does* work correctly - I don't think I fully rebuilt
my entire project...

Lasse_Vågsæther_Karlsen
5/9/2004 9:44:53 PM
On Sat, 8 May 2004 19:24:40 -0700, Tom Jones <tomjones16@hotmail.com>
[quoted text, click to view]

To simplify your regex'es, you can replace as follows:

* --> .*
? --> .
.. --> \.

Thus "???.a" becomes "^...\.a$"

--
Lasse Vågsæther Karlsen
mailto:lasse@vkarlsen.no
http://www.vkarlsen.no/
AddThis Social Bookmark Button