删除 uuid4 字符串模式
Remove uuid4 string pattern
我有以下字符串示例
1# 00000 Gin-a19ea68e-64bf-4471-b4d1-44f6bd9c1708-62fa6ae2-599c-4ff1-8249-bf6411ce3be7-83930e63-2149-40f0-b6ff-0838596a9b89 Kin
2# 00000 Gin-a19ea68e-64bf-4471-b4d1-44f6bd9c1708 Kin
我正在尝试删除 uuid4 生成的字符串以及 python 中 uuid4 字符串模式右侧的任何文本。
两个例子中的输出应该是00000 Gin
我在这里查看过What is the correct regex for matching values generated by uuid.uuid4().hex?。但是还是没用。
您可以使用:
import re
strings = ["00000 Gin-a19ea68e-64bf-4471-b4d1-44f6bd9c1708-62fa6ae2-599c-4ff1-8249-bf6411ce3be7-83930e63-2149-40f0-b6ff-0838596a9b89 Kin",
"00000 Gin-a19ea68e-64bf-4471-b4d1-44f6bd9c1708 Kin"]
rx = re.compile(r'^[^-]+')
# match the start and anything not - greedily
new_strings = [match.group(0)
for string in strings
for match in [rx.search(string)]
if match]
print(new_strings)
# ['00000 Gin', '00000 Gin']
参见 a demo on ideone.com。
要实际 检查 如果您的字符串是所需的格式,您可以使用以下表达式:
^
(?P<interesting>.+?) # before
(?P<uid>\b\w{8}-(?:\w{4}-){3}\w{12}\b) # uid
(?P<junk>.+) # garbage
$
在 regex101.com 上查看此演示(注意修饰符!)。
我有以下字符串示例
1# 00000 Gin-a19ea68e-64bf-4471-b4d1-44f6bd9c1708-62fa6ae2-599c-4ff1-8249-bf6411ce3be7-83930e63-2149-40f0-b6ff-0838596a9b89 Kin
2# 00000 Gin-a19ea68e-64bf-4471-b4d1-44f6bd9c1708 Kin
我正在尝试删除 uuid4 生成的字符串以及 python 中 uuid4 字符串模式右侧的任何文本。
两个例子中的输出应该是00000 Gin
我在这里查看过What is the correct regex for matching values generated by uuid.uuid4().hex?。但是还是没用。
您可以使用:
import re
strings = ["00000 Gin-a19ea68e-64bf-4471-b4d1-44f6bd9c1708-62fa6ae2-599c-4ff1-8249-bf6411ce3be7-83930e63-2149-40f0-b6ff-0838596a9b89 Kin",
"00000 Gin-a19ea68e-64bf-4471-b4d1-44f6bd9c1708 Kin"]
rx = re.compile(r'^[^-]+')
# match the start and anything not - greedily
new_strings = [match.group(0)
for string in strings
for match in [rx.search(string)]
if match]
print(new_strings)
# ['00000 Gin', '00000 Gin']
参见 a demo on ideone.com。
要实际 检查 如果您的字符串是所需的格式,您可以使用以下表达式:
^
(?P<interesting>.+?) # before
(?P<uid>\b\w{8}-(?:\w{4}-){3}\w{12}\b) # uid
(?P<junk>.+) # garbage
$
在 regex101.com 上查看此演示(注意修饰符!)。