Validate Matches in Procedural Code (用程序码验证匹配)

需求:

获取字符串Do you like 2, 3,4 , 6?中的2, 4, 6


方法

1. Python

a. Global function

import re

subject = "Do you like 2, 3,4 , 6?"

result = []

for matchobj in re.finditer(r"\d+",subject):

if int(matchobj.group()) % 2 == 0:

result.append(matchobj.group())


print result


b. Compiled object

import re

subject = "Do you like 2, 3,4 , 6?"

result = []


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

for matchobj in reobj.finditer(subject):

if int(matchobj.group()) % 2 == 0:

result.append(matchobj.group())


print result


2. Tcl

set subject "Do you like 2, 3,4 , 6?"

set result ""

set pos 0

while {[regexp -indices -start $pos -linestop {\d+} $subject offsets]==1} {
set pos [expr {1+[lindex $offsets 1]}]

set sTmp[string range $subject [lindex $offsets 0] [lindex $offsets 1]]

if {$sTmp % 2 == 0} {

lappend result $sTmp
}

}

foreach i $result {

puts "$i"

}

相关文章

正则替换html代码中img标签的src值在开发富文本信息在移动端...
正则表达式
AWK是一种处理文本文件的语言,是一个强大的文件分析工具。它...
正则表达式是特殊的字符序列,利用事先定义好的特定字符以及...
Python界一名小学生,热心分享编程学习。
收集整理每周优质开发者内容,包括、、等方面。每周五定期发...