簡體   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