need help on regex

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi,
i'm a new bie to regular expressions..
i'm trying to see if a string ends with teh below
pattern \filename.xls or \filename.xlsx...

I've used the @"\\[\w|\W]+\.xls" pattern but's it's
matching hte below string
d:\sample\.xls....

can someone help me out with teh correct pattern
 
Hello AVL,
hi,
i'm a new bie to regular expressions..
i'm trying to see if a string ends with teh below
pattern \filename.xls or \filename.xlsx...
I've used the @"\\[\w|\W]+\.xls" pattern but's it's
matching hte below string
d:\sample\.xls....
can someone help me out with teh correct pattern

The reason why it matches is quite easily explained.

the middle part of your expression [\w\W] matches any character. So this
will always match anything up to .xls at the end.

You can solve this issue with regular expresions with the following expression:

[^\\]\.xlsx?$

Though it may be even easier to use the System.IO.Path class and use it's
members like: HasExtention, Extention etc to query the data. That way you
won't have to worry yourself with URI parsing.
 
Back
Top