MySQL REGEXP 和重复的单词 [英] MySQL REGEXP and repeated words
问题描述
我有一个特定的问题.
我有,例如:你好小,你好红色,有时和现在
.
我需要正则表达式,只有当子句包含 hello
和 red
时才会匹配.
I need regexp what will be match only if the clause contains both hello
and red
.
我已经完成了一个正则表达式 ((hello|red).*){2}()*
只有在子句中有一个词 hello
和一个词时才有效red
,但在我的子句中,两个词 hello
和 regexp
将在没有词 red
的情况下工作,有什么问题:(
I have complete a regexp ((hello|red).*){2}()*
what will work only if in clause one word hello
and one word red
, but in my clause two words hello
and regexp
will work without word red
, what wrong:(
我需要正则表达式,仅当原因包含单词 hello
和单词 red
并且不依赖于重复其中之一时才匹配.
I need regexp what will match only if cause contain word hello
and word red
and not depend of repeat one of them.
请帮忙.
谢谢.
推荐答案
通常,人们会使用 positive前瞻断言用于此任务,但 MySQL 的正则表达式引擎不支持它们.
Usually, one would use positive lookahead assertions for this task, but MySQL's regex engine doesn't support them.
因此,您唯一的选择(如果您想在单个正则表达式中执行此操作)是同时处理这两种变体(red
或 hello
之后的hello
code> red
之前)手动":
Therefore, your only option (if you want to do this in a single regex) is to handle both variations (hello
after red
or hello
before red
) "manually":
hello.*red|red.*hello
对于两个搜索词",这可能是可以接受的——不过,它不能很好地扩展.
For two "search words", that's probably acceptable - it doesn't scale nicely, though.
你的正则表达式 ((hello|red).*){2}()*
有点奇怪;这意味着
Your regex ((hello|red).*){2}()*
is a bit strange; it means
( # 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 foo hello
或 red bar red
.
so this matches hello foo hello
or red bar red
as well.
这篇关于MySQL REGEXP 和重复的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!