regex help

B

Brian

i have the following regex:

Regex r = new
System.Text.RegularExpressions.Regex(@"LES_GAS[\d]{8}\-[\d]{6,7}\.DEL");

I expected it to match filenames like the following:
LES_GAS20080718-0301826.DEL

and it does....but can anyone tell me why this returns true:
bool b = r.IsMatch(@"C:\dir a\dir b\dir c\LES_GAS20080718-0301826.DEL");

it's like it just looks at the end of the string.
 
H

Hans Kesting

After serious thinking Brian wrote :
i have the following regex:

Regex r = new
System.Text.RegularExpressions.Regex(@"LES_GAS[\d]{8}\-[\d]{6,7}\.DEL");

I expected it to match filenames like the following:
LES_GAS20080718-0301826.DEL

and it does....but can anyone tell me why this returns true:
bool b = r.IsMatch(@"C:\dir a\dir b\dir c\LES_GAS20080718-0301826.DEL");

it's like it just looks at the end of the string.

The regex match is usually (=unless you specify otherwise) a
substring-match. If you had any text following that ".DEL", then it
still would match.

The solution: start your expression with a "^" to signify that the
match *has* to start at the beginning of the strign, and end it with
"$" to tell it that it *has* to match until the end of the string.

so:
new Regex(@"^LES_GAS[\d]{8}\-[\d]{6,7}\.DEL$");

Hans Kesting
 

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

Regex in C# 4
Regex help needed 1
Regex confusion 2
Help with Regex 5
Newbie question about Regex 8
Regex help 1
Regex Question 3
Regex : handling single quotes while parsing csv file 4

Top