推荐学习书目
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 · Jul 29, 2018 · 2862 views
    This topic created in 2864 days ago, the information mentioned may be changed or developed.

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

    5 replies    2018-08-01 21:13:56 +08:00
    lanpong
        1
    lanpong  
       Jul 29, 2018
    用的 py 版本是什么,py2 的话就是 Unicode 的问题,也就是中文编码
    bestehen
        2
    bestehen  
    OP
       Jul 29, 2018
    @lanpong 我用其他编码就匹配不到汉字?
    lanpong
        3
    lanpong  
       Jul 30, 2018
    @bestehen #2
    你需要了解一下编码。http://wklken.me/posts/2013/08/31/python-extra-coding-intro.html
    所以我还是推荐 Python3,不需要折腾这些问题
    msg7086
        4
    msg7086  
       Jul 30, 2018
    u 决定了你字符串的编码方式,而不是匹配的内容。
    也就是说,u 改变的是你的正则表达式,而不是 s 与正则表达式之间的关系。
    0mza987
        5
    0mza987  
       Aug 1, 2018
    楼主需要了解下编码知识
    http://pycoders-weekly-chinese.readthedocs.io/en/latest/issue5/unipain.html
    看完这篇你就懂了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5687 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 5414617a · 36ms · UTC 01:35 · PVG 09:35 · LAX 18:35 · JFK 21:35
    ♥ Do have faith in what you're doing.