2011-04-26 195 views
0

我想提取與"or"連接的單詞序列。例如,從正則表達式

"there or is or a or problem with my computer" 

我想提取

"there or is or a or problem" 

我有以下的正則表達式

(("[^"]+"+|.[^\s*]+)\s+or\s+)+("[^"]+"+|.[^\s*]+) 

但表情是給下面的結果:

"there or is", " a or problem or with" 

打破罪惡gle角色。這個表情有什麼問題嗎?

+0

這是什麼類的?我們在一兩天前就已經這樣做了,並且由於相同的原因,建議的解決方案仍然是錯誤的。 – tchrist 2011-04-26 20:55:40

+0

我們將其用於我們的全文搜索邏輯。 – Santosh 2011-04-26 21:11:49

+0

我將它改爲((「[^」] +「+ |。?[^ \ s *] +)\ *] +) 已添加?之後。並按預期工作 – Santosh 2011-04-27 21:55:59

回答

0

它連接什麼是字母拼寫的單詞,也可以是這樣的:

\w+(?:\s+or\s+\w+)* 

這將返回

"there or is or a or problem", "with", "my", "computer" 

如果你真的想只有那些至少有一個or在它,如你的例子,

\w+(?:\s+or\s+\w+)+ 

將返回

"there or is or a or problem" 
0

嘗試下面的一個:

[\w\s]+or\s+\w+ 

注意,這將匹配突出表現在以下:

有或或或問題,我的電腦或我我快瘋了

但是,如果你想要那裏或有或有問題,電腦或我以上,配合:

(\w+(?:\s+or\s+\w+)+)