推荐学习书目
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
fortunezhang
V2EX  ›  Python

python 正则找到规律数据

  •  1
     
  •   fortunezhang · Nov 14, 2014 · 4617 views
    This topic created in 4204 days ago, the information mentioned may be changed or developed.
    字符串: <Data>1=1&2=2</Data><Data>abc</Data><Data>nihao</Data>
    我想找到得到的是['1=1&2=2','abc','nihao']
    请各位帮忙解答一下,本人python菜鸟,勿喷,谢谢。
    6 replies    2014-11-15 00:09:55 +08:00
    c
        1
    c  
       Nov 14, 2014   ❤️ 1
    re.findall('<Data>([^<]+)</Data>', s)
    Kai
        2
    Kai  
       Nov 14, 2014 via iPhone   ❤️ 1
    移动到 /go/python
    ChanneW
        3
    ChanneW  
       Nov 14, 2014
    def tsplit(string, delimiters):
    """Behaves str.split but supports multiple delimiters."""

    delimiters = tuple(delimiters)
    stack = [string,]

    for delimiter in delimiters:
    for i, substring in enumerate(stack):
    substack = substring.split(delimiter)
    stack.pop(i)
    for j, _substring in enumerate(substack):
    stack.insert(i+j, _substring)

    return stack

    s ="<Data>1=1&2=2</Data><Data>abc</Data><Data>nihao</Data>"
    tsplit(s, (',', '<Data>', '</Data>'))
    uJohnny
        4
    uJohnny  
       Nov 14, 2014
    如果只是标签里的数据, 用lxml吧.
    不想用的话, 就参考下这个: http://bit.ly/1qHjIeV
    fortunezhang
        5
    fortunezhang  
    OP
       Nov 14, 2014
    @Kai 不知道怎么移动了。下次注意。thx
    irosyking
        6
    irosyking  
       Nov 15, 2014
    正则表达式为 (?<=<data>)(.*?)(?=<\/data>)

    import re

    m=re.findall(r'(?<=<data>)(.*?)(?=<\/data>)','<Data>1=1&2=2</Data><Data>abc</Data><Data>nihao</Data>',re.I|re.M)

    print m
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5103 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 09:22 · PVG 17:22 · LAX 02:22 · JFK 05:22
    ♥ Do have faith in what you're doing.