简体   繁体   中英

Create a python regular expression for finding 1 to 4 chars uppercase

I already have the following regular expression:

'([A-Z0-9]{1,4}(?![A-Z0-9]))'

that meets the following requirements.

  • 1-4 Characters in length
  • All Uppercase
  • Can be a mix of numbers and letters

Now Say I have this string "This is A test of a TREE, HOUSE"

result = ['T', 'A', 'TREE']

I don't want the first 'T' because it is not on it's own and is part of a word.

How would I go about modifying the re search to account for this?

Thanks

[Edit: Spelling]

You can use word boundaries \\b around your pattern.

>>> import re
>>> s = 'This is A test of a TREE, HOUSE'
>>> re.findall(r'\b[A-Z0-9]{1,4}\b', s)
['A', 'TREE']

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