字符串 s="hello 1234 world xx 上海 18 悠悠",用正则过滤掉英文和数字
得到:上海 悠悠

sub 过滤

re正则表达式中,过滤字符可以用替换的方法,用sub查找替换字符

import re

s = "hello 1234 world xx 上海 18 悠悠"
r = re.sub('[a-zA-Z0-9]', '', s)
print(r.strip())

运行结果:上海 悠悠

sub是查找替换,找到英文和数字[a-zA-Z0-9],替换成空字符,替换后首位会有空格
去掉首尾空格用 python 里面的 strip() 方法

详情参考这篇https://www.cnblogs.com/yoyoketang/p/14261349.html

正则提取中文

如果是正则提取字符串中的中文,可以匹配中文[\u4e00-\u9fa5]

s = "hello 1234 world xx 上海 18 悠悠"
r = re.findall(r'[\u4e00-\u9fa5]+', s)
print(" ".join(r))

运行结果:上海 悠悠

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-24
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-07-03
  • 2022-12-23
  • 2022-12-23
  • 2021-09-25
  • 2021-12-16
  • 2021-12-24
相关资源
相似解决方案