Alex Rivera | Logout

extract fileName using Regex

Asked 2009-07-08T01:31:49.973
12

If I want to match only fileName, i.e,

in C://Directory/FileName.cs, somehow ignore everything before FileName.cs using Regex.

How can I do it?

I need this for a Compiled UI I am working on ... can't use programming language as it only accepts Regex.

Any ideas?

Edit
Report

1 Answer

20

Something like this might work:

[^/]*$

It matches all characters to the end of the line that are not "/"..

If you want to match paths that use the "\" path separator you would change the regex to:

[^\]*$

But do make sure to escape the "\" character if your programming language or environment requires it. For instance you might have to write something like this:

[^\\]*$

EDIT I removed the leading "/" and trailing "/" as they may be confusing since they are not really part of the regEx but they are very common of representing a regular expression.

And of course, depending on the features that the regEx engine supports you may be able to use look-ahead/look-behind and capturing to craft a better regEx.

answered 2009-07-08T01:35:15.227

Your Answer