user2104778
user2104778

Reputation: 1040

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.

Upvotes: 2

Views: 2541

Answers (2)

Inbar Rose
Inbar Rose

Reputation: 43447

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)

Upvotes: 0

mgilson
mgilson

Reputation: 309929

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

Upvotes: 8

Related Questions