正则表达式:括号中的必需字符 [英] regex: required character in brackets
问题描述
如何从包括 x
的集合中搜索仅包含字符的字符串,并要求它包含 x?例如[a-z]+
但如果不包含 x
则不匹配.
How do I search for a string that contains only chars from a set including x
, and require it to contain x? e.g. [a-z]+
but not matching if it doesn't contain x
.
所以它应该匹配 quux
而不是 foo
或 bar
.
So it should match quux
but not foo
or bar
.
推荐答案
假设你想匹配一个只有 x,y 或 z 的字符串,匹配 start (^
) 后跟零个或多个 y
或 z
([yz]*
) 后跟一个 x
后跟零个或多个 x
、y
或 z
([xyz]*
) 后跟 end ($
)
Assuming you want to match a string of only x,y or z, match start (^
) followed by zero or more y
or z
([yz]*
) followed by an x
followed by zero or more x
, y
or z
([xyz]*
) followed by end ($
)
^[yz]*x[xyz]*$
如果您尝试匹配 [a-z]
但在某处带有 x
,那么应该这样做
If you are trying to match [a-z]
but with an x
in there somewhere, then this should do it
^[a-z]*x[a-z]*$
这篇关于正则表达式:括号中的必需字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!