比如有 aa(bbb)ccc
我只会一个 \(.*\)
但是这样会把圆括号也带上, 即输出: (bbb)
有没有能只取出圆括号里面的内容的正则? 不带圆括号...
我只会一个 \(.*\)
但是这样会把圆括号也带上, 即输出: (bbb)
有没有能只取出圆括号里面的内容的正则? 不带圆括号...
|
rmingApr 21, 2015
1
|
|
weyouApr 21, 2015
2
用正则的group啊, \((.*)\)
|
|
weyouApr 21, 2015
3
>>> import re
>>> a = "this is a (test), isn't it" >>> re.search(r'\((.*)\)', a).group(1) 'test' |
|
4
@weyou 感谢~ 如果是仅 .group() 不是 group(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)一样,返回的是全部的匹配内容 |
|
6
>>> import re
>>> re.search(r"(?<=\()\S+?(?=\))", "aa(bbb)ccc").group() |
|
200ccApr 21, 2015
7
var s = 'aa(bbb)ccc';
var r = /\((.*)\)/g; var result = r.exec(s)[1]; // bbb |
|
8
@wmttom 赞!非常感谢!
|
|
9
@zhyu 是的~ 只不过之前那位兄弟的正则 还是没把圆括号去掉
|