Regex confusion

T

Tom Jones

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
 
T

Tom Jones

Tom Jones said:
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

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

=?iso-8859-15?Q?Lasse_V=E5gs=E6ther_Karlsen?=

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').

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

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top