简体   繁体   中英

Regex - Optional Positive Lookbehind

I'm trying to match such that all the characters after the last / and before . gets matched. My current challenge is that . is only sometimes present.

I have an example here: https://regex101.com/r/ThWZwX/3

Where I'm hoping to match the 'match' text in both scenarios.

Thanks,

You can use negated character class in a capture group without any need of a lookahead:

.*\/([^.]*)

RegEx Demo

We use .*\\/ to match last / by using a greedy match of .* and then we use negated character class [^.]* to match until we get a dot or everything if dot is not found.

Also note that we use ([^.]*) to capture this match.

No need for anything complex

/([^/.]*)(?!.*/)

https://regex101.com/r/KHyXcJ/1

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM