Python 使用正则表达式替换

标签 python regex

例如,有谁知道如何用“\r\n<\d+”替换所有出现的“<\d+”正则表达式

"<20"

应该转化为

"\r\n<20"

但是

"> HELLO < asassdsa"

应该保持不变

最佳答案

>>> import re
>>> str = "<20"
>>> output = re.sub(r'<(?=\d)', r'\r\n<', str)
>>> output
'\r\n<20'

关于Python 使用正则表达式替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9192416/

相关文章:

python - 添加具有相同键的值

python - CSV 日期解析的 Dask 性能慢?

python - PasteDeploy 与 uwsgi 一起使用时抛出 TypeError

Python-Levenshtein 距离错误 "Assertion failed!"

python - 如何在 python 中将正则表达式序列化为 json?

python - 缩放并仅保存 matplotlib geopandas 图中感兴趣的中心部分

sql - 从 Oracle SQL 中的 URL 获取域

Python正则表达式匹配字符串或空格的开头

正则表达式 BNF 语法

regex - RegexBuddy 的免费替代品