Find a Match Within Another Match (在一个匹配中寻找另一个匹配)

前端之家收集整理的这篇文章主要介绍了Find a Match Within Another Match (在一个匹配中寻找另一个匹配)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

需求:

获取字符串1 <b>2</b> 3 4 <b>5 6 7</b>中的2,5, 6, 7


方法

1. Python

import re

subject = '1 <b>2</b> 3 4 <b>5 6 7</b>'

list = []

innerre = re.compile("\d+")

for outermatch in re.finditer("(?s)<b>(.*?)</b>",subject):

list.extend(innerre.findall(outermatch.group(1)))


print list


2. Tcl

set subject "1 <b>2</b> 3 4 <b>5 6 7</b>" set result "" set pos 0 while {[regexp -nocase -indices -start $pos {<b>(.*?)</b>} $subject offsets]==1} { set pos [expr {1+[lindex $offsets 1]}] set sTmp [string range $subject [lindex $offsets 0] [lindex $offsets 1]] set ios 0 while {[regexp -nocase -indices -start $ios -linestop {\d+} $sTmp offsets1]==1} { set ios [expr {1+[lindex $offsets1 1]}] set iTmp [string range $sTmp [lindex $offsets1 0] [lindex $offsets1 1]] lappend result $iTmp } } puts "result: $result"

猜你在找的正则表达式相关文章