简体   繁体   中英

Extract string from regex

For the below python code, I am using regex to parse the string. However I am struggling to extract the string from the matched pattern.

import re

rx = re.compile(
    r'^(?P<interesting>.+?)-(?P<uid>\b\w{8}-(?:\w{4}-){3}\w{12}\b)(?P<junk>.+)$',
    re.MULTILINE | re.VERBOSE)

test_str = u"00000 Gin-12-a19ea68e-64bf-4471-b4d1-44f6bd9c1708-62fa6ae2-599c-4ff1-8249-bf6411ce3be7-83930e63-2149-40f0-b6ff-0838596a9b89 Kin\n00000 Gin-a19ea68e-64bf-4471-b4d1-44f6bd9c1708 Kin\ntest123 test 12345678-1234-1234-1234-123456789012 junk afterwards\n"
tmp = re.findall(rx, test_str)
print(tmp[0])

I get the below output

('00000 Gin-12', 'a19ea68e-64bf-4471-b4d1-44f6bd9c1708', '-62fa6ae2-599c-4ff1-8249-bf6411ce3be7-83930e63-2149-40f0-b6ff-0838596a9b89 Kin')

My expected ouput is

00000 Gin-12

You have a named group in your regex so just use it:

import re

rx = re.compile(r'^(?P<interesting>.+?)-(?P<uid>\b\w{8}-(?:\w{4}-){3}\w{12}\b)(?P<junk>.+)$', re.MULTILINE | re.VERBOSE)

test_str = u"00000 Gin-12-a19ea68e-64bf-4471-b4d1-44f6bd9c1708-62fa6ae2-599c-4ff1-8249-bf6411ce3be7-83930e63-2149-40f0-b6ff-0838596a9b89 Kin\n00000 Gin-a19ea68e-64bf-4471-b4d1-44f6bd9c1708 Kin\ntest123 test 12345678-1234-1234-1234-123456789012 junk afterwards\n"
tmp = re.match(rx, test_str)
print(tmp.groupdict()["interesting"])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM