Retrieve a list of all matches (提取所有匹配的列表)

前端之家收集整理的这篇文章主要介绍了Retrieve a list of all matches (提取所有匹配的列表)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

需求:

获取字符串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)

b. Compiled object

import re

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

result = reobj.findall(subject)


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]] }

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