2013-10-23 36 views
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 

回答

2

如果定義了「字」是「非空白字符的字符串」,那麼這似乎這樣的伎倆:

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

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

+0

+1有一個良好的回答。 – anubhava

0

也許是這樣的?

(\w+\s+){1,10}\s*keyword\s*(\w+\s+){1,10}