RangerWolf

晕了,求助一个正则如何写,要把圆括号里面的内容取出来

  •  
  •   RangerWolf · Apr 21, 2015 · 3353 views
    This topic created in 4221 days ago, the information mentioned may be changed or developed.
    比如有 aa(bbb)ccc
    我只会一个 \(.*\)
    但是这样会把圆括号也带上, 即输出: (bbb)
    有没有能只取出圆括号里面的内容的正则? 不带圆括号...
    9 replies  •  2015-04-21 18:08:56 +08:00
    weyou
    weyou
    Apr 21, 2015
    2
    用正则的group啊, \((.*)\)
    weyou
    weyou
    Apr 21, 2015
    3
    >>> import re
    >>> a = "this is a (test), isn't it"
    >>> re.search(r'\((.*)\)', a).group(1)
    'test'
    RangerWolf
    RangerWolf
    OP
    Apr 21, 2015
    4
    @weyou 感谢~ 如果是仅 .group() 不是 group(1)的话 还是会带圆括号
    zhyu
    zhyu
    Apr 21, 2015❤️1
    5
    @RangerWolf
    re.MatchObject.group([group1, ...])

    Returns one or more subgroups of the match. If there is a single argument, the result is a single string; if there are multiple arguments, the result is a tuple with one item per argument. Without arguments, group1 defaults to zero (the whole match is returned).

    .group()和.group(0)一样,返回的是全部的匹配内容
    wmttom
    wmttom
    Apr 21, 2015❤️1
    6
    >>> import re
    >>> re.search(r"(?<=\()\S+?(?=\))", "aa(bbb)ccc").group()
    200cc
    200cc
    Apr 21, 2015
    7
    var s = 'aa(bbb)ccc';
    var r = /\((.*)\)/g;
    var result = r.exec(s)[1]; // bbb
    RangerWolf
    RangerWolf
    OP
    Apr 21, 2015
    8
    @wmttom 赞!非常感谢!
    RangerWolf
    RangerWolf
    OP
    Apr 21, 2015
    9
    @zhyu 是的~ 只不过之前那位兄弟的正则 还是没把圆括号去掉
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4488 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 06:10 · PVG 14:10 · LAX 22:10 · JFK 01:10
    ♥ Do have faith in what you're doing.