how extract the alphanumeric value using regex

import re
text  = """THE PO NUMBER 134314 MUST BE INCLUDED ALONG WITH
SERVICE DATE ON ALL INVOICES SUBMITTED THROUGH THE AVID BILL PAY PROCESS.
THANK YOU."""

p_no = re.search("PO# (\w+)|PO NUMBER (\w+)", text)
print(p_no)
if p_no == None:
    print("No value found")
else:
    print(p_no.group(1))

REQUIRED SOLUTION : 134314

Is there any solution to get the text after PO number but it should also take alphanumeric like B14786,AC7823 etc

>Solution :

You can put PO# and PO NUMBER in an alternation pattern in a non-capturing group so that there’s only one capturing group to deal with and that p_no.group(1) will always contain a value no matter which of PO# and PO NUMBER matches:

p_no = re.search(r"(?:PO#|PO NUMBER) (\w+)", text)

Leave a Reply