[英]Regex: match between last occurrence of a character and first occurrence of another character
I have the following string:我有以下字符串:
COUNTRY/CITY/street_number_floor_tel
I'd like to extract what is after the last occurrence of '/' and first occurrence of '_'.我想提取最后一次出现'/'和第一次出现'_'之后的内容。 So the result is:
所以结果是:
street
So far I've managed to come up with this regex:到目前为止,我已经设法想出了这个正则表达式:
[^/]+(?=_)
which results in the following:结果如下:
street_number_floor
So I basically don't know how to stop after finding the first occurrence of '_'.所以我基本上不知道在找到第一次出现'_'之后如何停止。
Many thanks for any hints in advance!非常感谢您提前提供的任何提示!
You may use您可以使用
(?<=/)[^/_]+(?=_[^/]*$)
See the regex demo查看正则表达式演示
Details细节
(?<=/)
- a positive lookbehind requiring a /
immediately before the match (?<=/)
- 需要在比赛前立即使用/
的积极回溯[^/_]+
- 1+ chars other than /
and _
[^/_]+
- 除/
和_
之外的 1+ 个字符(?=_[^/]*$)
- a positive lookahead that requires _
, then 0+ chars other than /
till the end of string immediately to the right of the current location. (?=_[^/]*$)
- 一个正向前瞻,需要_
,然后是除/
之外的 0+ 个字符,直到紧挨当前位置右侧的字符串结尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.