简体   繁体   中英

How to find all the exact matches in a given string using python Regex

I am trying to fetch all the 3 character words from my string, but getting only first occurrence

import re
a="AAA BBBBBBBBBB CCCCCCC DDD FFF"
print(re.findall('(^[A-Z]{3})',a))

Actual output:

['AAA']

Expected Output is:

['AAA','DDD','FFF']

^[AZ]{3} will match only 3 characters from the start of the string.

Try re.findall(r'\b[AZ]{3}\b', a) which will match word boundaries appropriately.

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