I already have the following regular expression:
'([A-Z0-9]{1,4}(?![A-Z0-9]))'
that meets the following requirements.
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.