Python Forum
Failing regex, space before and after the "match"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Failing regex, space before and after the "match"
#1
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!
Reply
#2
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
Reply
#3
I need to catch only one line:
Visual ID: Z2BB417600504 <--- Need to get this line
Reply
#4
(Mar-05-2023, 06:11 AM)tester_V Wrote: I need to catch only one line:
Visual ID: Z2BB417600504 <--- Need to get this line
Reply
#5
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.
tester_V likes this post
Reply
#6
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!
Reply
#7
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} ")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Failing to connect by 'net use' tester_V 1 601 Apr-20-2024, 06:31 AM
Last Post: tester_V
  Facing issue in python regex newline match Shr 6 3,030 Oct-25-2023, 09:42 AM
Last Post: Shr
  Regex pattern match WJSwan 2 1,958 Feb-07-2023, 04:52 AM
Last Post: WJSwan
  Failing regex tester_V 3 1,741 Aug-16-2022, 03:53 PM
Last Post: deanhystad
  Match substring using regex Pavel_47 6 2,030 Jul-18-2022, 07:46 AM
Last Post: Pavel_47
  Match key-value json,Regex saam 5 6,314 Dec-07-2021, 03:06 PM
Last Post: saam
  Failing to connect to a host with WMI tester_V 6 5,331 Aug-10-2021, 06:25 PM
Last Post: tester_V
  Failing to get Stat for a Directory tester_V 11 4,587 Jul-20-2021, 10:59 PM
Last Post: Larz60+
  Failing to Zip files tester_V 4 2,793 Dec-01-2020, 07:28 AM
Last Post: tester_V
  regex.findall that won't match anything xiaobai97 1 2,423 Sep-24-2020, 02:02 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020