- 论坛徽章:
- 0
|
回复 3# zhasm
谢谢,但是如果用包含数字的字符串去替换还是会出错:- >>> line='source_host=1.1.1.1'
- >>> a=re.compile(r'(source_host=).*')
- >>> b='abcd'
- >>> a.sub(r'\1%s' %b ,line)
- 'source_host=abcd'
- >>> b='1.2.3.4'
- >>> a.sub(r'\1%s' %b ,line)
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- File "/usr/local/python/lib/python2.6/re.py", line 278, in filter
- return sre_parse.expand_template(template, match)
- File "/usr/local/python/lib/python2.6/sre_parse.py", line 795, in expand_template
- raise error, "invalid group reference"
- sre_constants.error: invalid group reference
- >>>
- >>> b='202.101.10.1'
- >>> a.sub(r'\1%s' %b ,line)
- 'P2.101.10.1'
- >>>
复制代码 |
|