trying to create a regex that finds some letter combinations follow by numbers. Like ABC123, however the it could be ABC 123, ABCID:123
reg = re.compile('(ABC(?:ID\:| )?\d*?)(?:$| |,|\t)',re.IGNORECASE)
This works, however it also finds ABC by itself..
Is there way only find it if followed by numbers?
Thanks in advance
You use a
*? quantifier with
\d, and it matches 0 or more digits (as few as possible).
You may use
See the regex demo
[a-z]+to match 1 or more ASCII letters (note that compiling the pattern with
re.Iflag will enable
[a-z]to also match the uppercase ASCII letters, too))
(?:ID:| )?- 1 or 0 occurrences of
ID:or a space
\d+- 1+ digits (the
+is a greedy quantifier, so there is no need to define the right-hand context with
Note that if you plan to also match Unicode letters in Python 2.x, you need to use
re.findall(r'[^\W\d_]+(?:ID:| )?[0-9]+', s, flags=re.U|re.I)
re.U flag will make
\d classes Unicode-aware.
You can use: