Does anyone know how to replace all ocurences of '<\d+' regex with '\r\n<\d+', for example
"<20"
should be transformed to
"\r\n<20"
but
"> HELLO < asassdsa"
shuld be untouched
>>> 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)
Or using lookahead:
import re
def replace(value):
return re.sub(r'(?=<\d)', r'\r\n', value)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With