我有一个特别的问题。
我有,例如:Hello little, hello red, sometime and now
。
我需要regexp只有当子句同时包含hello
和red
时才匹配的内容。
我已经完成了一个regexp((hello|red).*){2}()*
如果在子句中只有一个单词hello
和一个单词red
可以工作,但是在我的子句中,两个单词hello
和regexp
没有单词red
可以工作,有什么问题:(
我需要regexp只有当原因包含单词hello
和单词red
而不依赖于重复其中一个时才匹配。
请帮忙。
谢谢。
最佳答案
通常,人们会使用positive lookahead assertions来完成这个任务,但是MySQL的regex引擎不支持它们。
因此,您唯一的选择(如果您希望在单个regex中这样做)是手动处理这两种变体(hello
afterred
或hello
beforered
):
hello.*red|red.*hello
对于两个“搜索词”来说,这可能是可以接受的——不过,它并不能很好地扩展。
你的regex有点奇怪,意思是
( # Start of group:
(hello|red) # Match either hello or red
.* # Match any number of characters
){2} # Match this group twice
()* # Match the empty string any number of times...
所以这也与
((hello|red).*){2}()*
或hello foo hello
匹配。关于mysql - MySQL REGEXP和重复的单词,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23465799/