1
banbanchs 2014-11-22 15:43:24 +08:00 1
加括号,比如\<(.*)\>
|
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 |
4
14 2014-11-22 15:49:55 +08:00
哈,这个问题好熟悉,以前刚用re,很纠结匹配之后还要去切片甚至多次re多次切片,直到我知道了()之后眼泪掉下来
|
5
tommark OP @banbanchs 不好意思,我自己弄错了,刚才用这个网站http://tool.oschina.net/regex试验结果不对,用python重新做了一下是对了
|
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. |
8
xz 2014-11-22 18:04:34 +08:00
a="aaaa<ab>xxx"
re.findall("<(.+)>",a) |
9
ryd994 2014-11-23 07:30:57 +08:00 via Android
regex look ahead/behind
|
10
ryd994 2014-11-23 07:31:50 +08:00 via Android
这题这么简单还用不上capture
|
11
leoleozhu 2014-11-23 07:51:14 +08:00 via Android
前后环视
|
12
cdxem713 2014-11-23 08:30:59 +08:00 via iPhone
顺带一问,如果是在js里面要达到同样效果,有没有办法呀
|
13
mhohai 2014-11-24 08:51:55 +08:00
正则,你知道捕获括号和非捕获括号么?不想匹配,你知道有四种环视么?
知道一个会有问题么? 啥都不知道,就别说会正则! ————没错,我是愤青! |