繁体   English   中英

如何只匹配和标记两个单词,但它们之间有一个字符串?

[英]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 向我显示关键字? 感谢您提供任何意见!

我想你想搜索这个:

.*?(sentence).{0,50}?(beautiful).*

然后将其替换为:

\1 \2

在这里测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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