Mar-05-2023, 06:25 AM
Why are you adding all the extra whitespace? Only look for the important stuff.
import re line = " Visual ID: Z2BB417600504 " pattern = r"Visual ID:\s+(Z[A-Z0-9]{12})" print(re.search(pattern, line).group(1))
Output:Z2BB417600504
If you want to match the entire string for some reason.import re line = " Visual ID: Z2BB417600504 " pattern = r"^\s*Visual ID:\s+Z[A-Z0-9]{12}\s*$" print(re.search(pattern, line))
Output:<re.Match object; span=(0, 39), match=' Visual ID: Z2BB417600504 '>
That pattern looks a lot like yours. Not sure why you think it doesn't work. Could you post how you are using your pattern and it fails.