08-数据提取-正则表达式

2019-03-06  本文已影响0人  Vanna_bot
  • re模块的常见方法
  • 原始字符串r
  • 匹配中文

re模块的常见方法

ret = re.findall("\d","chuan1zhi2")
>>['1', '2']
re.sub("\d","_","wu1xuan2")
>>wu_xuan_
p = re.compile("\d",re.S)
p.findall("chuan1zhi2")

python中原始字符串r的用法

原始字符串(raw string):保持原先字符串中所有的字符
如:“\n”的原始字符串就是“\\n”

len("\n")
>>1
len(r"\n")
>>2

匹配中文

中文 unicode 编码范围:[u4e00-u9fa5](不包含中文标点)
注意:汉字和正则表达式都需要是unicode字符操作
【练习】提取中文

# coding:utf-8
import re

title="<p>Look out your window and I`ll be gone</p> <p>看向你的窗外我早已离开</p> <p>You`re the reason I`m traveling on</p> <p>因为你我才四处漂泊</p> "

p = re.findall(r"[\u4E00-\u9FA5]+",title)
print(p)
>>['看向你的窗外我早已离开', '因为你我才四处漂泊']
上一篇下一篇

猜你喜欢

热点阅读