# 用\1取得第一个括号的内容
# 用法:将123与456换位置
import re
content='Extra strings Hello 123 456 World_This is a Regex Demo Extra strings'
# content=re.sub('(Extra.*?)(\d+)(\s)(\d+)(.*?strings)',r'\1\4\3\2\5',content)
c=re.search('(\d+)(\s)(\d+)',content)
print(c.group(1,2,3))
content=re.sub('(\d+)(\s)(\d+)',r'\3\2\1',content)
print(content)
"""
('123', ' ', '456')
Extra strings Hello 456 123 World_This is a Regex Demo Extra strings
"""
网友评论