简体   繁体   中英

python regex matching specific word only, not a subset

I'm trying to search for specific words using regex in python.

lst2 = ['Azmat', 'AZ', 'azim', 'Zard', 'Zardari']

pattern = re.compile(r"\bAZ|Zard\b", re.I)

for item in lst2:
    if re.search(pattern, item):
        print item

This code produces:

Azmat
AZ
azim
Zard

Why is it not matching "AZ" and "Zard" only?

It's because your regex is matching either:

\bAZ

OR

Zard\b

Use a non-capture group to limit the 'influence' of the | operator:

\b(?:AZ|Zard)\b

This way, it reads: \\b then either AZ OR Zard and last \\b .

Your current code is looking for a word starting with az or finishing with zard . Fix it like this:

pattern = re.compile(r"\b(AZ|Zard)\b", re.I)

What about:

pattern = re.compile(r"^(AZ|Zard)$", re.I)

better show start and end of string with ^ and $

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