tags:

views:

486

answers:

5

Hello everyone,

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?

+3  A: 

What language are you using? Why are you not using the standard path mechanisms of that language?

How about http://msdn.microsoft.com/en-us/library/system.io.path.aspx ?

Tom Leys
This. Specifically the GetFileName() method.
Turnor
I agree, api first, regex second.
Mercer Traieste
+4  A: 

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.

Miky Dinescu
You always have to use two backslashes in the regex to match one in the target string. If you're using a language like Java that doesn't provide regex literals or raw/verbatim strings, you have to double-escape them: "[^\\\\]*"
Alan Moore
lol i spent like an hour messing around with lookbehinds and finaly got a regex with like 100 characters to work... then i saw yours... deleted all the look behinds and it still worked. :P
Victor
A: 

I would use: ./(.$)

The parenthesis mark a group wich is the file name. The regular expression you use may vary dependig on the regex syntax(PCRE, POSIX)

I sugest you use a regex tool, there are several for windows and linux:

Windows - http://sourceforge.net/projects/regexcreator/

Windows - http://weitz.de/regex-coach/

Linux - kodos

Hope it helps

jpbaudoin
+1  A: 

Based on your comment of needing to exclude paths that do not match 'abc', try this:

^.+/(?:(?!abc)[^/])+$


Completely split out in regex comment mode, that is:

(?x)     # flag to enable comments
^        # start of line

.+       # match any character (except newline)
         #   greedily one or more times
/        # a literal slash character

(?:      # begin non-capturing group
  (?!      # begin negative lookahead
           # (contents must not appear after the current position)
    abc      # literal text abc
  )        # end negative lookahead
  [^/]     # any character that is not a slash
)        # end non-capturing group
+        # repeat the above nc group one or more times
         #   (essentially, we keep looking for non-backspaces that are not 'abc')

$        # end of line
Peter Boughton
A: 

just a variation on miky's that works for both filesystem path characters: [^\\/]*\s

Victor