2011-04-26 36 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+)+)