python3中的re库使用

目录

  • 1 基础使用

1 基础使用

(一)

程序如下,

#==============更多关于正则表达式的信息,可以参考Python官方文档:https://docs.python.org/3/library/re.html
import re

line = "replace sdlink froms. sdlinkid = 1526093896, before = 1527148286,1542948896,1542962446,1586022115,1526200502,1526469937, after = 1527148286,1526469937,1542948896,1542962446,1586022115,1526200502"
pattern = r"sdlink (\w+).*?sdlinkid = (\d+), before = ([\d,]+), after = ([\d,]+)"

match = re.search(pattern, line)
if match:
    datatype = str(match.group(1))
    sdlinkid = int(match.group(2))
    before = str(match.group(3))
    after = str(match.group(4)) 

print("type:", datatype)
print("sdlinkid:", sdlinkid)
print("before:", before)
print("after:", after)

输出如下,

type: froms
sdlinkid: 1526093896
before: 1527148286,1542948896,1542962446,1586022115,1526200502,1526469937
after: 1527148286,1526469937,1542948896,1542962446,1586022115,1526200502 

你可能感兴趣的:(Python学习,python,numpy,开发语言)