[英]Python regex - Match words only containing A, B, or C
What regex expression can I use to match words that are made of up ONLY the characters A, B, or C? 我可以使用什么正则表达式来匹配由字符A,B或C组成的单词? For example the regex would catch ABCBACBACBABBABCC and A and B and C but would not catch ABCD, ABC1, etc.
例如,正则表达式将捕获ABCBACBACBABBABCC和A和B和C,但不会捕获ABCD,ABC1等。
What about \\b[ABC]+\\b
? 怎么样
\\b[ABC]+\\b
? Does that work? 那样有用吗?
>>> regex = re.compile(r'\b[ABC]+\b')
>>> regex.match('AACCD') #No match
>>> regex.match('AACC') #match
<_sre.SRE_Match object at 0x11bb578>
>>> regex.match('A') #match
<_sre.SRE_Match object at 0x11bb5e0>
\\b
is a word boundary. \\b
是单词边界。 So here we match anything that is a word boundary followed by only A
, B
or C
characters until the next word boundary. 所以在这里我们匹配任何单词边界,然后只有
A
, B
或C
字符,直到下一个单词边界。
For those who don't like regex, we can use set
objects here as well: 对于那些不喜欢正则表达式的人,我们也可以在这里使用
set
对象:
>>> set("ABC").issuperset("ABCABCABC")
True
>>> set("ABC").issuperset("ABCABCABC1")
False
The regular expression you are looking for is r'\\b([ABC]+)\\b'
. 您正在寻找的正则表达式是
r'\\b([ABC]+)\\b'
。
You can compile it: 你可以编译它:
>>> regex = re.compile(r'\b([ABC]+)\b')
and then you can do some things with it: 然后你可以用它做一些事情:
>>> regex.match('ABC') # find a match with whole string.
>>> regex.search('find only the ABC') # find a match within the whole string.
>>> regex.findall('this will find only the ABC elements in this ABC test text') # find 2 matches.
If you want to ignore the case, then use: 如果要忽略大小写,请使用:
>>> regex = re.compile(r'\b([ABC]+)\b', re.I)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.