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
HDecember
V2EX  ›  Python

[问题]求解一个python正则的跨行匹配的问题

  •  
  •   HDecember · 2013-10-23 13:46:27 +08:00 · 5223 次点击
    这是一个创建于 3838 天前的主题,其中的信息可能已经有所发展或是发生改变。
    http://v2.freep.cn/3tb_131023134156kqj9512293.jpg

    urlContent1 获取不到匹配的内容,但是urlContent2可以 两个只差一个空行,
    在正则上也加了跨行属性,为什么不能匹配呢?求解

    ——————以下是图片中的代码的文本部分——————

    urlContent1 ='''

    111
    222
    '''
    urlContent2 ='''111
    222
    '''
    global_pattern = re.compile('111',re.S|re.I|re.M)
    globalMatch = global_pattern.match(urlContent1)
    if globalMatch:
    globalContent = globalMatch.group()
    print 'urlContent1 match' + globalMatch.group()

    globalMatch = global_pattern.match(urlContent2)
    if globalMatch:
    globalContent = globalMatch.group()
    print 'urlContent2 match' + globalMatch.group()
    2 条回复    1970-01-01 08:00:00 +08:00
    funagi
        1
    funagi  
       2013-10-23 13:59:01 +08:00   ❤️ 1
    match()是从起始位置开始匹配,urlContent1起始位置是换行符,当然匹配不到了,应该用search()。
    详见:http://wiki.ubuntu.org.cn/Python%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E6%93%8D%E4%BD%9C%E6%8C%87%E5%8D%97#match.28.29_vs_search.28.29
    HDecember
        2
    HDecember  
    OP
       2013-10-23 14:06:52 +08:00
    喔喔~~~~好的~~~谢谢~~~!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2837 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 02:51 · PVG 10:51 · LAX 19:51 · JFK 22:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.