繁体   English   中英

Python替换使用正则表达式

[英]Python replace using regex

有没有人知道如何用'\\ r \\ n <\\ d +'替换'<\\ d +'正则表达式的所有ocurence,例如

"<20"

应该转变为

"\r\n<20"

"> HELLO < asassdsa"

shuld不受影响

>>> import re
>>> str = "<20"
>>> output = re.sub(r'<(?=\d)', r'\r\n<', str)
>>> output
'\r\n<20'
import re
def replace(value):
  return re.sub(r'(<\d)', r'\r\n\1', value)

或使用前瞻:

import re
def replace(value):
  return re.sub(r'(?=<\d)', r'\r\n', value)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM