python - 在 python 中使用正则表达式/替换替换文本中的字母数字
问题描述
我想删除文本中的字母数字字符。例如,我有如下文字:
text= I want to remove alphanumeric jhanb562nkk from the text. Remove alphanumeric from all the texts. uhufshfn76429 is very hard to figure out.
预期结果
result=I want to remove alphanumeric from the text. Remove alphanumeric from all the texts. is very hard to figure out.
我不确定我们如何使用 regex/replace 方法从文本中删除它们。
解决方案
您可以使用以下正则表达式:
[A-Za-z]+[\d]+[\w]*|[\d]+[A-Za-z]+[\w]*
函数调用将是:
re.sub(rgx_str, '', text)
请注意,这将在清除字母数字文本的地方留下额外的空间。删除此问题的一种简单方法是运行另一个正则表达式以进行后处理:
" +"
并替换为" "
.
推荐阅读
- c# - (已解决)如何在函数中创建可用于所有其他函数的变量?
- windows - 如何从以管理员身份运行的更新程序/脚本更新 Windows 任务计划程序任务
- python - 如何将一个模块内的类中的 pyqtSignal 连接到另一个模块内的类中的 pyqtSlot?
- github - 删除分支并搞砸备份..需要恢复或跳下桥
- node.js - nodejs如何处理trycatch中的函数错误
- ios - SwiftUI:单一目标中的 iPhone、iPad、Mac
- firebase - Firebase 功能在部署后不起作用(MAC - 操作系统)
- mongodb - 更新 MongoDB 中的实体时,避免单次执行中的多个事务
- c# - 将自定义命名空间导入 ASPX
- c++ - 如何使用 Arduino 代码将 modbus 帧解码为浮点值?