regex help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?QnJpYW4=?=

    regex help

    i have the following regex:

    Regex r = new
    System.Text.Reg ularExpressions .Regex(@"LES_GA S[\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_GAS200807 18-0301826.DEL");

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

    #2
    Re: regex help

    After serious thinking Brian wrote :
    i have the following regex:
    >
    Regex r = new
    System.Text.Reg ularExpressions .Regex(@"LES_GA S[\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_GAS200807 18-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_GA S[\d]{8}\-[\d]{6,7}\.DEL$");

    Hans Kesting


    Comment

    Working...