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"
#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


Messages In This Thread
RE: Failing regex, space before and after the "match" - by deanhystad - Mar-05-2023, 06:25 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Failing to connect by 'net use' tester_V 1 323 Apr-20-2024, 06:31 AM
Last Post: tester_V
  Facing issue in python regex newline match Shr 6 1,565 Oct-25-2023, 09:42 AM
Last Post: Shr
  Regex pattern match WJSwan 2 1,382 Feb-07-2023, 04:52 AM
Last Post: WJSwan
  Failing regex tester_V 3 1,298 Aug-16-2022, 03:53 PM
Last Post: deanhystad
  Match substring using regex Pavel_47 6 1,561 Jul-18-2022, 07:46 AM
Last Post: Pavel_47
  Match key-value json,Regex saam 5 5,566 Dec-07-2021, 03:06 PM
Last Post: saam
  Failing to connect to a host with WMI tester_V 6 4,572 Aug-10-2021, 06:25 PM
Last Post: tester_V
  Failing to get Stat for a Directory tester_V 11 3,741 Jul-20-2021, 10:59 PM
Last Post: Larz60+
  Failing to Zip files tester_V 4 2,266 Dec-01-2020, 07:28 AM
Last Post: tester_V
  regex.findall that won't match anything xiaobai97 1 2,099 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