I'm looking for a regex to achieve the following:
When my input is root:Folder1:fname
I want to get fname
and when the input string is root:Folder1:'fname'
to get again fname
(no quotes!) .
For the former [^:?]*$
seems to get the job done but I cannot find a single regex to match both cases.
Is it possible to do it with a single regex? Tried to achieve it with conditional regex but I couldn't make it.
Any other tip/solution is welcome.
Thank you.
edit: The format of the paths is not fixed, quotes might appear elsewhere (eg root:'Folder with spaces1':fname or root:'Folder with spaces1':'fname'). The solution I guess is to search backwards as the filename we want to capture is always after the last colon.
As you tagged pcre, you might also use a branch reset group to get group 1 between single quotes or not between single quotes.
^.*:(?|'(\w+)'|(\w+))$
^
Start of string .*:
Match until the last :
(?|
Branch reset group
'(\w+)'
Capture 1+ word chars between single quotes |
Or (\w+)
Capture 1+ word chars)
Close branch reset group $
End of string This will work if your "grammar" is strict:
(\w+:)*'?(\w+)'?
Watch the regex debugger here: https://regex101.com/r/SjoWYP/1
If you want to enforce paired quotes, you need to capture the first quote.
(\w+:)*('?)(\w+)\2$
The expression inside the third pair of parentheses will be available as the third captured group ( $3
or \3
or match.group(3)
or what have you).
If your regex dialect permits non-grouping parentheses, you can rearticulate this as
(?:\w+:)*('?)(\w+)\1$
and/or also perhaps extend the quote group to permit either single or double quote ( ['"]?
pro '?
). Then of course the final group will be $2
instead of $3
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.