Python Forum
Failing regex, space before and after the "match" - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Failing regex, space before and after the "match" (/thread-39541.html)



Failing regex, space before and after the "match" - tester_V - Mar-05-2023

Greetings to those that do not sleep! Wink
I'm trying to get the line that goes like this:
    Visual ID:          Z2BB417600504  <--- Need to get this line
But it also could be found like this:
Visual ID:          Z2BB417600504 
or like this :
Some lines from the file Visual ID:  Z2BB417600504 and also
Another line Visual ID: Z2BB417600504 from the same file
Regex I constructed does not work for some reason.
re.search("^\s+Visual ID:\s+Z[A-Z0-9]{12}\s+$",line) 


Could anyone explain why the hell regex is not working?
I used "^\s+" because of the multiple spaces before the " Visual ID:"
Then "\s+" for spaces after the " Visual ID:"
Then "Z[A-Z0-9]{12}" to match the visual ID number
And the "\s+$" to match the spaces at the end of the visual number.

seems right but it is not.... Sad

Any help is appreciated! Thank you!


RE: Failing regex, space before and after the "match" - bowlofred - Mar-05-2023

Do you need the whole line, or just the ID after the "visual ID" text?

I'd ignore anything before it rather than trying to match something in particular. Will it always be the last thing on a line? If not, don't try to match to end of line.

import re

with open("visualid.txt") as f:
    for lineno, line in enumerate(f, 1):
        m = re.search(r"Visual ID:\s*(Z[A-Z0-9]{12})", line)
        if m:
            print(f"Found ID {m.group(1)} on line {lineno}")
Output:
Found ID Z2BB417600504 on line 2 Found ID Z2BB417600504 on line 4 Found ID Z2BB417600504 on line 6 Found ID Z2BB417600504 on line 7



RE: Failing regex, space before and after the "match" - tester_V - Mar-05-2023

I need to catch only one line:
Visual ID: Z2BB417600504 <--- Need to get this line


RE: Failing regex, space before and after the "match" - tester_V - Mar-05-2023

(Mar-05-2023, 06:11 AM)tester_V Wrote: I need to catch only one line:
Visual ID: Z2BB417600504 <--- Need to get this line



RE: Failing regex, space before and after the "match" - deanhystad - Mar-05-2023

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.


RE: Failing regex, space before and after the "match" - tester_V - Mar-05-2023

I do not know what happened. I replaced your my virsion of regex with yours and now it is working.
ef = 'c:/0001/Visual_ID.txt'        
with open(ef,'r') as fl :
    for rn_l in fl :
        rn_l.strip()
        if re.search("\s+Visual ID:\s+Z[A-Z0-9]{12}\s+$",rn_l)  : 
            print(f" matched -> {rn_l}")         
            #print(f"LINE {rn_l}")
            *gb,vid = rn_l.split(":")
            vid=vid.strip()                    
            print(f" VISUALID Number={vid} ")
It prints the line I need. I'm a happy camper now Wink
Thank you! You guys are the best!


RE: Failing regex, space before and after the "match" - deanhystad - Mar-06-2023

You are not getting much benefit from using re.search.
        if re.search("\s+Visual ID:\s+Z[A-Z0-9]{12}\s+$",rn_l)  : 
            print(f" matched -> {rn_l}")         
            #print(f"LINE {rn_l}")
            *gb,vid = rn_l.split(":")
            vid=vid.strip()                    
            print(f" VISUALID Number={vid} ")
Let re.search extract the information for you.
        if match := re.search("Visual ID:\s+(Z[A-Z0-9]{12})", rn_l)  : 
            vid = match.group(1)          
            print(f" VISUALID Number={vid} ")