请问这个正则表达式如何写 - 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
sicongliu

请问这个正则表达式如何写

  •  
  •   sicongliu May 28, 2015 3900 views
    This topic created in 4006 days ago, the information mentioned may be changed or developed.
    <dt><a name="313"></a>ADHE 313 (6) <b>Organization of Adult Basic Education Programs</b></dt>

    想抓出ADHE313和Organization of Adult Basic Education Programs
    18 replies    2015-05-29 10:45:43 +08:00
    asj
        1
    asj  
       May 28, 2015
    这难道不是应该用CSS/JQuery selector,或者XPath么?
    phx13ye
        2
    phx13ye  
       May 28, 2015   1
    <\/a>(.*)<b>(.*?)<\/b>
    sicongliu
        3
    sicongliu  
    OP
       May 28, 2015
    xpath比较简单 但是想学下正则的方法
    shoumu
        4
    shoumu  
       May 28, 2015
    看一下pyquery吧,支持jQuery的语法
    professorz
        5
    professorz  
       May 28, 2015
    .+<\\/a>(.+)(6)<b>(.+)<\\/b>.+
    java下的regex
    sicongliu
        6
    sicongliu  
    OP
       May 28, 2015
    python的如何写
    yiyiwa
        7
    yiyiwa  
       May 28, 2015
    python测试了一下,不完善,有空的东西。

    '\>([^\<]*)\<'
    sicongliu
        8
    sicongliu  
    OP
       May 28, 2015
    m=re.search("</a>(.*?)\s(",text)
    print (m.group(1))

    m=re.search("<b>(.*?)</b>(",text)
    print (m.group(1))
    sicongliu
        9
    sicongliu  
    OP
       May 28, 2015
    如果要取ADHE 313呢?
    如何判断第二个空格?当然用字符串的search切片功能很容易达到,只是想知道正则如何达到
    sicongliu
        10
    sicongliu  
    OP
       May 28, 2015
    m=re.search("</a>(.*?)\s+\(",text)
    print (m.group(1))

    当然方法比较笨,如果第二个空格后不是“(”就没办法了
    asj
        11
    asj  
       May 28, 2015
    简单写了一个,还很不完善
    (?:<dt.*?>)(?:.*?\/.*?>)([\w ]*)(?:.*?)(?:<\/dt>)

    http://regexr.com/3b3bs
    fy
        12
    fy  
       May 28, 2015
    这个需求不用正则,会简单得多

    page.xpath("//dt/text()") -> ADHE 313 (6)
    page.xpath("//dt/b/text()") -> Organization of Adult Basic Education Programs
    picasso250
        13
    picasso250  
       May 28, 2015
    /a>([\w ()]+)<b>([\w ]+)</b>
    最简单的解决了你现在的问题。
    picasso250
        14
    picasso250  
       May 28, 2015
    对不起,上一个是错误的,多提取了(6)

    /a>(\w+ \d+).+?<b>([\w ]+)</b>
    leozy2014
        15
    leozy2014  
       May 28, 2015
    print re.findall('</a>(.*?) \(6\) <b>(.*?)</b></dt>', s)
    #[('ADHE 313', 'Organization of Adult Basic Education Programs')]
    wmttom
        16
    wmttom  
       May 28, 2015
    python正则 (?<=>)[\w, ,\(,\)]+?(?= \(|<)

    re.findall("(?<=>)[\w, ,\(,\)]+?(?= \(|<)", '<dt><a name="313"></a>ADHE 313 (6) <b>Organization of Adult Basic Education Programs</b></dt>')

    ['ADHE 313', 'Organization of Adult Basic Education Programs']
    sicongliu
        17
    sicongliu  
    OP
       May 29, 2015
    楼上两个貌似都不能用
    sicongliu
        18
    sicongliu  
    OP
       May 29, 2015
    sorry这个可行

    print re.findall('</a>(.*?) \(6\) <b>(.*?)</b></dt>', s)
    #[('ADHE 313', 'Organization of Adult Basic Education Programs')]
    About     Help     Advertise     Blog     API     FAQ     Solana     2764 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 44ms UTC 01:52 PVG 09:52 LAX 18:52 JFK 21:52
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86