2.7.6 python下,\S大写S的不能匹配到中文?

来源:3-3 正则表达式-2

LNYIE

2018-03-07

#\S  大写S用途
line = "你很好"
regex_str = "(你\S好)"    #\S代表  "你""好"之间  除了空格什么都可以
match_obj = re.match(regex_str,line)
if match_obj:
   print "yes"
   print match_obj.group()    
   print match_obj.group(0)

在2.7.6下

你s好  可以用  你\S好 匹配到

你很好  就不能用 你\S好 匹配到



3.5下 试了一下可以匹配到


是python版本原因? 2.7版本的\S不能匹配中文吗 ?

写回答

1回答

bobby

2018-03-12

这是由于python2.7和python3对编码 不一致造成的 你可以试试 在python2.7中讲字符串设置会

line = u"你很好"

和regex_str = u"(你\S好)"然后再试试


0
1
LNYIE
非常感谢!
2018-03-12
共1条回复

Scrapy打造搜索引擎 畅销4年的Python分布式爬虫课

带你彻底掌握Scrapy,用Django+Elasticsearch搭建搜索引擎

5817 学习 · 6291 问题

查看课程