0

我有一段文字。我想匹配一个单词并选择它前面的 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
4

2 回答 2

2

如果您将“单词”定义为“非空白字符的字符串”,那么这似乎可以解决问题:

((\S+\s){,10})(standard)((\s\S+){,10})

演示:http ://rubular.com/r/a3SsQZbCkP

于 2013-10-23T10:31:38.520 回答
0

也许是这样的?

(\w+\s+){1,10}\s*keyword\s*(\w+\s+){1,10}
于 2013-10-23T10:27:11.343 回答