Anthony
Anthony

Reputation: 742

Getting the last backslash in a filepath via regex

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...?

Upvotes: 5

Views: 7879

Answers (3)

Sergey Galashyn
Sergey Galashyn

Reputation: 6956

What about

<cfset fileName = GetFileFromPath("\\server\folder_A\folder_B\etc\more.mov") />

Upvotes: 7

GWB
GWB

Reputation: 2605

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

<cfset fileName = ListLast(filePath, "\\")>

Upvotes: 5

Brian Roach
Brian Roach

Reputation: 76918

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)

Upvotes: 6

Related Questions