![](/img/trans.png)
[英]How to match string in between two words, but only the “closet” of the two words?
[英]How to match and mark only two words, but with a string between them?
我想匹配“sentence”和“beautiful”,只有这两个词并且只有当它们彼此相距很近,但没有它们之间的词时。
例子是:
This is a sentence, it is beautiful.
我想匹配“句子”和“美丽”,如果它们彼此接近并且只有它们。 我尝试过的一切,我总是匹配它们之间的一切,或者我必须制作两个捕获组。
尝试:
\b(?i)(?:sentence.{0,50}beautiful)\b
\b(?i)(?:(sentence).{0,50}(beautiful))\b
\b(?i)(?:sentence(?=.{0,50}beautiful))\b
结果:
Full Match: sentence, it is beautiful
Full Match: sentence, it is beautiful, Group 1: sentence, Group 2: beautiful
Full Match: sentence
我还尝试了其他变体,但我无法只向我展示我的关键字“句子美丽”,而它们之间没有任何内容。 是否可以仅使用 Regex 向我显示关键字? 感谢您提供任何意见!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.