我有一段文字。我想匹配一个单词,然后选择它前面的10个单词和跟随它的10个单词以及关键字本身。
到目前为止,我已经达到以下选择之前的30个字符和之后的30个字符。怎么可以转换,所以我实际上可以选择10个单词和之后的10个单词?
.{1,30}?keyword.{1,30}
示例:
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum
has been the industry's standard dummy text ever since the 1500s, when an unknown printer
took a galley of type and scrambled it to make a type specimen book. It has survived not
only five centuries, but also the leap into electronic typesetting, remaining essentially
unchanged. It was popularised in the 1960s with the release of Letraset sheets containing
Lorem Ipsum passages, and more recently with desktop publishing software like Aldus
PageMaker including versions of Lorem Ipsum.
我希望匹配standard
。正则表达式应返回以下匹配
printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer
答案 0 :(得分:2)
如果你定义“word”是一个“非空白字符串”,那么这似乎可以解决问题:
((\S+\s){,10})(standard)((\s\S+){,10})
答案 1 :(得分:0)
也许是这样的?
(\w+\s+){1,10}\s*keyword\s*(\w+\s+){1,10}