文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>《TCL 教程英文版》笔记§22 正则表达式 101

《TCL 教程英文版》笔记§22 正则表达式 101

时间:2008-08-27  来源:oychw

§22 正则表达式 101

 

两种正则表达式的使用方法:

 

regexp ?switches? exp string ?matchVar? ?subMatch1 ... subMatchN?

Searches string for the regular expression exp. If a parameter matchVar is given, then the substring that matches the regular expression is copied to matchVar. If subMatchN variables exist, then the parenthetical parts of the matching string are copied to the subMatch variables, working from left to right.

 

regsub ?switches? exp string subSpec varName

 

Searches string for substrings that match the regular expression exp and replaces them with subSpec. The resulting string is copied into varName.

            规则有^,$,.,*,+,[...],[^...],(...),其中(...)不是很熟悉

            正则表达式和16,18章中讲的globbing类似。不过globbing中只能使用*。Regexp和string match类似。

 

实例:

 

注意regexp好像只返回第一个匹配的值

# cat ch22

# !/bin/bash

# \

exec tclsh "$0" "$@"

 

set sample "Where there is a will, There is a way."

 

#

# Match the first substring with lowercase letters only

#

set result [regexp {[a-z]+} $sample match]

puts "Result: $result match: $match"

 

#

# Match the first two words, the first one allows uppercase

set result [regexp {([A-Za-z]+) +([a-z]+)} $sample match sub1 sub2 ]

puts "Result: $result Match: $match 1: $sub1 2: $sub2"

 

#

# Replace a word

#

regsub "way" $sample "lawsuit" sample2

puts "New: $sample2"

 

#

# Use the -all option to count the number of "words"

#

puts "Number of words: [regexp -all {[^ ]} $sample]"

运行结果

# ./ch22

Result: 1 match: here

Result: 1 Match: Where there 1: Where 2: there

New: Where there is a will, There is a lawsuit.

Number of words: 30

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载