我需要编写一个匹配字符串的正则表达式,该字符串具有三个字符之一,仅表示x,y和z。我试过"[xyz]^"
,但它不起作用。该字符串可能包含任何其他字符,但必须包含任何顺序或位置的三个给定字符中的至少一个
答案 0 :(得分:3)
\b\w*(x|y|z)\w*\b
\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)
答案 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')
>>>