Beach Williams
Beach Williams

Reputation: 153

Regex to select last word from path

I have some strings like the following:

C:/DB/UCMerced_LandUse/UCMerced_LandUse/Unfoldered_Images/airplane00.tif

I would like to keep only 'airplane'. For that, I've come up with the following regexes:

[^/]+$ - which would select 'airplane00.tif'

^\D* - which removes the last part (i.e. 'C:/DB/UCMerced_LandUse/UCMerced_LandUse/Unfoldered_Images/airplane')

How can I combine these two in order to get only 'airplane'? Or perhaps there is a better approach...

Thank you.

Upvotes: 1

Views: 1334

Answers (4)

The fourth bird
The fourth bird

Reputation: 163632

As an alternative you could match until the last occurrence of /, then capture any char except a forward slash or digit in a group ([^/\d]+)\d* and match any potentially following digits.

Then match a dot and any char except a forward slash.

The value is in capture group 1.

/([^/\d]+)\d*\.[^/]+$

Regex demo

Upvotes: 0

parap
parap

Reputation: 1985

Try

[^\]([a-zA-Z]+)[^\].*$

Then you just need to get the first group.

In python, all you need to do is add .group(1) to the end of your line.

check out this article about capturing groups

Upvotes: 0

ssc-hrep3
ssc-hrep3

Reputation: 16089

Use ([^\/]+)\.\D+$ and match the first capturing group.

It is probably better though to use some built-in functionality of your programming language to get the filename from the path. Also splitting the string would be a possibility.

Upvotes: 0

anubhava
anubhava

Reputation: 786291

You can use this regex and grab captured group #1

([^/\d]+)[^/]*$

RegEx Demo

[^/\d]+ matches 1 or more of any character that is not \d and /.

Alternatively, you can also use this regex with lookbehind and lookahead assertions:

(?<=/)[^/\d]+(?=[^/]*$)

Upvotes: 1

Related Questions