微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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"

原文地址:https://www.jb51.cc/regex/361724.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐