python 正则提取一个字符串时如何只返回字符串而不返回边界? - V2EX
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
tommark
V2EX    Python

python 正则提取一个字符串时如何只返回字符串而不返回边界?

  •  
  •   tommark 2014-11-22 15:37:20 +08:00 6458 次点击
    这是一个创建于 3977 天前的主题,其中的信息可能已经有所发展或是发生改变。
    例如匹配字符串“aaaa<ab>xxx"中尖括号中的ab,用‘\<.*\>'匹配后,返回的结果是<ab>,如何实现返回的结果只有ab呢?
    一种简单的方法是对返回的结果做切片,请问怎么用python正则表达可以做到?
    13 条回复    2014-11-24 08:51:55 +08:00
    banbanchs
        1
    banbanchs  
       2014-11-22 15:43:24 +08:00   1
    加括号,比如\<(.*)\>
    vulgur
        2
    vulgur  
       2014-11-22 15:46:39 +08:00
    p = re.compile(r'<(.*)>)
    m = p.search(string)
    if m:
    s = m.group(1)
    return s
    tommark
        3
    tommark  
    OP
       2014-11-22 15:48:25 +08:00
    @banbanchs 试了一下不管用啊
    14
        4
    14  
       2014-11-22 15:49:55 +08:00
    哈,这个问题好熟悉,以前刚用re,很纠结匹配之后还要去切片甚至多次re多次切片,直到我知道了()之后眼泪掉下来
    tommark
        5
    tommark  
    OP
       2014-11-22 15:53:50 +08:00
    @banbanchs 不好意思,我自己弄错了,刚才用这个网站http://tool.oschina.net/regex试验结果不对,用python重新做了一下是对了
    decken
        6
    decken  
       2014-11-22 16:49:25 +08:00 via Android
    @vulgur 如果是用sub替换字符串的时候这样写规则无效哦,还有什么办法吗
    aaaa007cn
        7
    aaaa007cn  
       2014-11-22 17:55:29 +08:00
    @decken
    RTFM
    https://docs.python.org/3/library/re.html#re.sub
    > Backreferences, such as \6, are replaced with the substring matched by group 6 in the pattern.
    > In string-type repl arguments, in addition to the character escapes and backreferences described above, \g<name> will use the substring matched by the group named name, as defined by the (?P<name>...) syntax. \g<number> uses the corresponding group number; \g<2> is therefore equivalent to \2, but isn’t ambiguous in a replacement such as \g<2>0. \20 would be interpreted as a reference to group 20, not a reference to group 2 followed by the literal character '0'. The backreference \g<0> substitutes in the entire substring matched by the RE.
    xz
        8
    xz  
       2014-11-22 18:04:34 +08:00
    a="aaaa<ab>xxx"
    re.findall("<(.+)>",a)
    ryd994
        9
    ryd994  
       2014-11-23 07:30:57 +08:00 via Android
    regex look ahead/behind
    ryd994
        10
    ryd994  
       2014-11-23 07:31:50 +08:00 via Android
    这题这么简单还用不上capture
    leoleozhu
        11
    leoleozhu  
       2014-11-23 07:51:14 +08:00 via Android
    前后环视
    cdxem713
        12
    cdxem713  
       2014-11-23 08:30:59 +08:00 via iPhone
    顺带一问,如果是在js里面要达到同样效果,有没有办法呀
    mhohai
        13
    mhohai  
       2014-11-24 08:51:55 +08:00
    正则,你知道捕获括号和非捕获括号么?不想匹配,你知道有四种环视么?
    知道一个会有问题么?

    啥都不知道,就别说会正则!

    没错,我是愤青!
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     855 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 26ms UTC 19:31 PVG 03:31 LAX 12:31 JFK 15:31
    Do have faith in what you're doing.
    ubao 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