python 正则子表达式组
文本:
<p><span>1</span></p>
<p><span>1</span><span>2</span></p>
<p><span>1</span><span>2</span><span>3</span></p>
正则:
<p>(<span>(\d+?)</span>){1,}?</p>
想得到的结果是
1
1,2
1,2,3
但python的groups里只能得到
1
2
3
谁知道解决办法?
[解决办法]
你这个工具应该搜了不仅一次的
你这句正则换在php得到的结果和python是一样的
$a='<p>0</p><p><span>1</span></p><p><span>1</span><span>2</span></p><p><span>1</span><span>2</span><span>3</span></p><div><span>div</span></div>';
preg_match_all('#<p>(?:<span>(\d+?)</span>){1,}?</p>#', $a, $m);
var_export($m);
array (
0 =>
array (
0 => '<p><span>1</span></p>',
1 => '<p><span>1</span><span>2</span></p>',
2 => '<p><span>1</span><span>2</span><span>3</span></p>',
),
1 =>
array (
0 => '1',
1 => '2',
2 => '3',
),
import regex as re
reg = re.compile(r'<p>(?:<span>(\d+?)</span>){1,}?</p>', re.I)
txt = '<p><span>1</span></p><p><span>1</span><span>2</span></p><p><span>1</span><span>2</span><span>3</span></p>'
for m in reg.finditer(txt):
print m.captures(1)