regex help

  • Thread starter Thread starter Brian
  • Start date Start date
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.
 
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
 
Back
Top