I wanted to extract PAN from the message
PAN=re.compile(r"[a-zA-Z]{5}[0-9]{4}[a-zA-Z]")
mes="ITR for AY 2019-20 for PAN: apzxxxxx9g has been processed at CPC"
abc=PAN.search(mes)
print(abc)
output: None
CodePudding user response:
Assuming you want to extract xxxxx9g
, you use 4
repetitions of the number, which cannot yield a correct match.
You can change it to 1 to 4 numbers ([0-9]{1,4}
):
PAN=re.compile(r'[a-zA-Z]{5}[0-9]{1,4}[a-zA-Z]')
mes="ITR for AY 2019-20 for PAN: apzxxxxx9g has been processed at CPC"
abc=PAN.search(mes)
print(abc)
output: <re.Match object; span=(31, 38), match='xxxxx9g'>
>>> print(abc.group())
xxxxx9g
CodePudding user response:
Your regex is looking for five alphabetic characters, followed by four digits, followed by one alphabetic. Nothing in your string matches (apzxxxxx9g
is close, but exactly three of the x
s immediately preceding 9
would need to be digits). Either your input needs to change, or your regex does.