加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Iterate over All the matches

发布时间:2020-12-14 01:46:16 所属栏目:百科 来源:网络整理
导读:需求: 打印字符串Do you like 12 or 34?中的12, 34 方法: 1. Python a. Global function import re subject = "Do you like 12 or 34?" result = re.findall(r"d+",subject) for i in result: print i, b. Compiled object import re reobj = re.compile

需求:

打印字符串Do you like 12 or 34?中的12, 34


方法:

1. Python

a. Global function

import re

subject = "Do you like 12 or 34?"

result = re.findall(r"d+",subject)
for i in result:

print i,


b. Compiled object

import re

reobj = re.compile(r"d+")

result = reobj.findall(subject)

for i in result:

print i,


引申: (返回match object)

for iter in re.finditer("d+",subject):

print iter,


2. Tcl

set subject "Do you like 12 or 34?"

set result ""

set pos 0

while {[regexp -indices -start $pos -linestop {d+} $subject offsets]==1} {
set pos [expr {1+[lindex $offsets 1]}]
lappend result [string range $subject [lindex $offsets 0] [lindex $offsets 1]]
}

foreach i $result {

puts "$i"

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读