正则表达式包含三个字符之一?

时间:2013-12-09 09:33:02

标签: regex

我需要编写一个匹配字符串的正则表达式,该字符串具有三个字符之一,仅表示x,y和z。我试过"[xyz]^",但它不起作用。该字符串可能包含任何其他字符,但必须包含任何顺序或位置的三个给定字符中的至少一个

3 个答案:

答案 0 :(得分:3)

Regex Demo

\b\w*(x|y|z)\w*\b

Regular expression visualization

Debuggex Demo

\b assert position at a word boundary (^\w|\w$|\W\w|\w\W)
\w* match any word character [a-zA-Z0-9_]
    Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
1st Capturing group (x|y|z)
    1st Alternative: x
        x matches the character x literally (case sensitive)
    2nd Alternative: y
        y matches the character y literally (case sensitive)
    3rd Alternative: z
        z matches the character z literally (case sensitive)
\w* match any word character [a-zA-Z0-9_]
    Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
\b assert position at a word boundary (^\w|\w$|\W\w|\w\W)
g modifier: global. All matches (don't return on first match)
m modifier: multi-line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

答案 1 :(得分:0)

这可能就是你要找的东西:

 ^.*[xyz].*$

Regular expression visualization

Debuggex Demo

答案 2 :(得分:0)

以下正则表达式应匹配:

^.*[xyz].*$

在python中:

>>> import re
>>> re.match(r'^.*[xyz].*$', 'AzE')
<_sre.SRE_Match object at 0x2643718>
>>> re.match(r'^.*[xyz].*$', 'AEz')
<_sre.SRE_Match object at 0x2643cc8>
>>> re.match(r'^.*[xyz].*$', 'AE')
>>>