Reputation: 153
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
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*\.[^/]+$
Upvotes: 0
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
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
Reputation: 786291
You can use this regex and grab captured group #1
([^/\d]+)[^/]*$
[^/\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