我试图编写一个正则表达式,匹配包含所有指定字母的任意单词。
例如:
a,b
Aaron: not match
Abby: match
Barry: match
Bobb: not match
Alley: not match
Abel: match
Bella: match
有什么想法吗?
我试图编写一个正则表达式,匹配包含所有指定字母的任意单词。
例如:
a,b
Aaron: not match
Abby: match
Barry: match
Bobb: not match
Alley: not match
Abel: match
Bella: match
(?=\w*a)(?=\w*b)\w+
在到达空格或非单词字符之前,请检查是否有“a”和“b”,然后捕获单词结束前的所有字符。
"no aa bb ab no aa bb ba".match(/(?=\w*a)(?=\w*b)\w+/g);
给我返回了 ["ab", "ba"]
。 - ColBeseder\w
或\S
比点更合适。 - ColBeseder\b...\b
中查找单词,这样更有意义。 - shA.tAaron bobby
,会发生什么? - ColBeseder(?=.*[aA])
。 - David Hoelzera,b
来说很好,但对于 a,b,c,d
来说就有点糟糕了 ;)。 - shA.t(\w*[Aa]\w*[bB]\w*)|(\w*[Bb]\w*[Aa]\w*)
a,b
来说不错,但对于 a,b,c,d
来说就有点糟糕了 ;)。 - shA.t这是我会使用的(Python语法):
word_with_a_and_b = re.compile(r"""
# Match word having both "a" and "b".
\b # Anchor to start of word.
(?=[^\Wa]*a) # Assert word contains an "a".
(?=[^\Wb]*b) # Assert word contains a "b".
\w+ # Match the word having both "a" and "b".
\b # Anchor to end of word.
""", re.VERBOSE | re.IGNORECASE)