python 中的正则表达式删除 2 个模式
regex in python to remove 2 patterns
想要制作一个正则表达式以删除字符串左侧的 2019 和 0 以及字符串右侧的最后六个零。
original value Dtype : class 'str'
original value: 2019 01 10 00 00 00
expected output is : 1 10
使用 str.split
和列表切片。
例如:
s = "2019 01 10 00 00 00"
print(" ".join(s.split()[1:3]).lstrip("0"))
使用re.match
例如:
import re
s = "2019 01 10 00 00 00"
m = re.match(r"\d{4}\b\s(?P<value>\d{2}\b\s\d{2}\b)", s)
if m:
print(m.group("value").lstrip("0"))
输出:
1 10
想要制作一个正则表达式以删除字符串左侧的 2019 和 0 以及字符串右侧的最后六个零。
original value Dtype : class 'str'
original value: 2019 01 10 00 00 00
expected output is : 1 10
使用 str.split
和列表切片。
例如:
s = "2019 01 10 00 00 00"
print(" ".join(s.split()[1:3]).lstrip("0"))
使用re.match
例如:
import re
s = "2019 01 10 00 00 00"
m = re.match(r"\d{4}\b\s(?P<value>\d{2}\b\s\d{2}\b)", s)
if m:
print(m.group("value").lstrip("0"))
输出:
1 10