- 论坛徽章:
- 11
|
回复 1# ssfjhh
应该看懂你意思了 - Python 3.3.4 (default, Feb 10 2014, 22:06:52)
- [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
- Type "help", "copyright", "credits" or "license" for more information.
- >>> import re
- >>> txt1 ="""a
- ... b
- ...
- ... c
- ...
- ...
- ... d"""
- >>> txt2 = re.sub(r'\n\n+', lambda e: (len(e.group()) - 1) * '\n\\\\newline' + '\n\n', txt1, flags=re.S)
- >>> txt1
- 'a\nb\n\nc\n\n\nd'
- >>> txt2
- 'a\nb\n\\\\newline\n\nc\n\\\\newline\n\\\\newline\n\nd'
- >>> print(txt1)
- a
- b
- c
- d
- >>> print(txt2)
- a
- b
- \\newline
- c
- \\newline
- \\newline
- d
- >>>
复制代码 |
|