简体   繁体   中英

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? For example the regex would catch ABCBACBACBABBABCC and A and B and C but would not catch ABCD, ABC1, etc.

What about \\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. So here we match anything that is a word boundary followed by only A , B or C characters until the next word boundary.


For those who don't like regex, we can use set objects here as well:

>>> set("ABC").issuperset("ABCABCABC")
True
>>> set("ABC").issuperset("ABCABCABC1")
False

The regular expression you are looking for is 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM