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