Chinaunix

标题: re.sub正则使用\number替换的问题 [打印本页]

作者: stlaw    时间: 2008-01-24 12:06
标题: re.sub正则使用\number替换的问题
用了大量的()检索文本,重分组各字符顺序,利用\number来替换,
例如:
newtxt=re.sub(r'%s' %re_sub,r'%s' %re_repl,oldtxt)
re_sub --
^(\d+),(\d+),(\d+),(\d+),(\d+),(\d+),(\d+),(\d+),(\d+),(\d+),(\d+),$
re_repl --
\1,\2,\3,\4,\5,\6,\7,\8,\9,\10,\11
这里遇到问题,竟然超过9,就认不出了,pythondoc 是写明支持到99的;
用m.group(10),m.group(11)是没问题的;
再找re的doc说明,里面有提到\number 也可以使用\g 来替换,试试之下,竟然可以--
re_repl --
\1,\2,\3,\4,\5,\6,\7,\8,\9,\g,\g
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/57278/showart_470447.html




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2