views:

115

answers:

3

Given a file path such as: \\server\folder_A\folder_B\etc\more.mov

I need a regex that'll give me the last backslash so I can extract the actual file name.

My attempt of "$\\" is not returning anything.

I'm using coldfusion.

Suggestions...?

+2  A: 

Do you just want everything after the last backslash (the filename)?

([^\\]+)$

The filename will be contained in the capture.

To match starting at the last backslash you'd do...

\\[^\\]+$

I'm not familiar with coldfusion, but I'm assuming that if it does regular expressions, it does captures as well. If you do really need the position and can get that from the match, the second expression might be what you want.

(Edited for clarity and to answer comment)

Brian Roach
No, just the position in the string of the last backslash. Although just grabbing the content after last backslash would work too.
Anthony
@Anthony: Well, regexp can't get you any "position", it can give you only matching substring. So if you want regexp I would just use what is provided in this answer (although there should be extra backslash `([^\\]+)$`)
serg
True - I should have been more precise in my question. I'm using the REFind function in coldfusion, which requires a regex to find the substring, and reports back the index where it finds it. It's Friday...
Anthony
Aside from an unmatched parenthesis, it works: (([^\\]+)$) Thanks
Anthony
@serg555, some regex functions in some languages return string positions of the match.
macek
+4  A: 

Do you absolutely have to use regex? Why not split the string and grab the last element?

<cfset fileName = ListLast(filePath, "\\")>
GWB
No, I don't. Thanks, never thought of the path as a list...
Anthony
+5  A: 

What about

<cfset fileName = GetFileFromPath("\\server\folder_A\folder_B\etc\more.mov") />
Sergii
I like this more than my own answer. The explicit function name improves readability and it probably handles more path separators than just \.
GWB