由买买提看人间百态

topics

全部话题 - 话题: nxxyyy
(共0页)
i********g
发帖数: 72
1
来自主题: Programming版 - 问个PYTHON烂问题
In your code, r'x\nxx' is pattern, and r'aaax\nxxyyy' is the string to match.
You need to remove 'r' to get right. r'aaax\nxxyyy' is raw string
interpreted as [ aaax\nxxyyy ], However, 'aaax\nxxyyy' is NOT raw string, '\
n' is interpreted as new line;
If the pattern isn’t found, meaning xNEWLINExx is not equal to [x\nxx],
string is returned unchanged. That's why you get your first results.
Make sure the pattern get match.
Hope this helps!

’r
y***a
发帖数: 840
2
来自主题: Programming版 - 问个PYTHON烂问题
为啥第一个没做替换? 如果是RAW STRING, 应该是MATCH的啊, 反而第二个没用’r
', 不是RAW STRING啊
1. >>> re.sub(r'x\nxx', 'zzz', r'aaax\nxxyyy')
'aaax\nxxyyy'
2. >>> re.sub(r'x\nxx', 'zzz', 'aaax\nxxyyy')
'aaazzzyyy'
(共0页)