需求:
获取字符串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"
原文链接:https://www.f2er.com/regex/361724.html