V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
bestehen
V2EX  ›  Python

Python 中正则匹配问题

  •  
  •   bestehen · 2018-07-29 16:39:55 +08:00 · 1957 次点击
    这是一个创建于 2069 天前的主题,其中的信息可能已经有所发展或是发生改变。

    ss=re.findall(u'(\d+)小时前',s) 这里的 u 是表示匹配汉字吗?为啥有人说里面有汉字必须用 u 呢?

    5 条回复    2018-08-01 21:13:56 +08:00
    lanpong
        1
    lanpong  
       2018-07-29 16:55:21 +08:00
    用的 py 版本是什么,py2 的话就是 Unicode 的问题,也就是中文编码
    bestehen
        2
    bestehen  
    OP
       2018-07-29 22:52:45 +08:00
    @lanpong 我用其他编码就匹配不到汉字?
    lanpong
        3
    lanpong  
       2018-07-30 10:35:15 +08:00
    @bestehen #2
    你需要了解一下编码。http://wklken.me/posts/2013/08/31/python-extra-coding-intro.html
    所以我还是推荐 Python3,不需要折腾这些问题
    msg7086
        4
    msg7086  
       2018-07-30 12:28:59 +08:00
    u 决定了你字符串的编码方式,而不是匹配的内容。
    也就是说,u 改变的是你的正则表达式,而不是 s 与正则表达式之间的关系。
    0mza987
        5
    0mza987  
       2018-08-01 21:13:56 +08:00
    楼主需要了解下编码知识
    http://pycoders-weekly-chinese.readthedocs.io/en/latest/issue5/unipain.html
    看完这篇你就懂了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4031 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 00:59 · PVG 08:59 · LAX 17:59 · JFK 20:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.