可以用?P<name>的方法给正则匹配的部分命名。
例:要将<字母,数字>的部分命名为test
x = "abc <haha,123> test @@"
pattern = "(?P<test>\<\w+,\d+\>)"
m = re.search(pattern, x)
r = m.group("test")
print r
输出:
<haha,123>
\<\w+,\d+\>)" m="re.search(pattern,x)r=m.group("test")printr输出: Posted 匡子语
tags: 篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python给正则匹配部分命名相关的知识,希望对你有一定的参考价值。 可以用?P<name>的方法给正则匹配的部分命名。 例:要将<字母,数字>的部分命名为test 输出: 以上是关于python给正则匹配部分命名的主要内容,如果未能解决你的问题,请参考以下文章
python给正则匹配部分命名
x = "abc <haha,123> test @@"
pattern = "(?P<test>\<\w+,\d+\>)"
m = re.search(pattern, x)
r = m.group("test")
print r
<haha,123>