Aug-16-2022, 03:53 PM
This kind of works.
A stricter match is possible if we are willing to specify the character that follows the string. This pattern says the string must be followed by something that is not normally part of a word (whitespace, punctuation).
import re text = ".BG13TPPV123..BG13TPPV2345...BG13TPPV3456B...BG13TPPV4567C...BG13TPPV7890D" print(re.findall(r"\w+\d{2}TPPV\d{4}[BC]?", text))
Output:['BG13TPPV2345', 'BG13TPPV3456B', 'BG13TPPV4567C', 'BG13TPPV7890']
Notice that it matches part of "BG13TPPV7890D" because "BG13TPPV7890" is a match to the pattern.A stricter match is possible if we are willing to specify the character that follows the string. This pattern says the string must be followed by something that is not normally part of a word (whitespace, punctuation).
import re text = ".BG13TPPV123..BG13TPPV2345...BG13TPPV3456B...BG13TPPV4567C...BG13TPPV7890D." print(re.findall(r"(\w+\d{2}TPPV\d{4}[BC]?)\W", text))
Output:['BG13TPPV2345', 'BG13TPPV3456B', 'BG13TPPV4567C']