简体   繁体   English

正则表达式:一个字符的最后一次出现和另一个字符的第一次出现之间的匹配

[英]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.

相关问题 正则表达式:匹配特定字符的第一次和最后一次出现 - Regex: match between the fisrt and last occurrence of a specific character 正则表达式提取字符的第一次出现和最后一次出现之间的字符串 - Regex to extract string between the first and last occurrence of a character 匹配正则表达式直到第一次出现特殊字符'-&gt;' - Match regex until the first occurrence of special character '->' JavaScript RegEx-替换字符的第一个和最后一个出现 - JavaScript RegEx - replace first and last occurrence of a character 正则表达式删除除第一次出现和最后一次出现的特殊字符 - regex remove special character except first occurrence and last occurrence ruby中的正则表达式,用于匹配字符的第一次出现和最后一次出现之间的文本 - Regular expression in ruby to match the text between first occurrence and last occurrence of a character 正则表达式:在第一次出现和最后一次出现之间获取 - Regex: Get between first occurrence of one and last occurrence of another 正则表达式:字符之前的最后一次出现 - Regex: last occurrence of character before 正则表达式:重复字符的最后一次出现 - Regex: Last Occurrence of a Repeating Character 正则表达式:确定在第一次出现另一个字符之前是否出现某个字符 - Regex: Determine if a character appears before first occurrence of another character
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM