1.*表示重复0次或多次,+表示重复一次或多次,?表示重复0次货一次
.*匹配任意字符串甚至是空字符串
2.TCL使用管道符|来自定交替匹配,如匹配hello Hello
可以用hello|Hello 或者(h|H)ello或者【h|H】ello
3.^表示行其实而不是字符串其实,$表示行结尾,\A,\Z反标匹配字符串的起始于终止位置。
4.匹配以反斜杠可以用\\或\B
5.使用()可以捕获子模式,非捕获(?:pattern)
6.\s匹配空格,\w匹配字母数字和下划线,\y匹配单词的起始和终止位置。
7.字符替换
string replace
接受一个字符串作为参数以及要删除的字符序列的考题和结尾,还可以接受一个字符串参数作为置换用字符串
string map
% string replace "12345" 2 4 "good"
12good
8.确定字符串类型
string is digit
string is control
默认清空下,如果字符串为空,对任何类型string is 都返回1.使用-strict选项可以强制要求在字符串为空时返回0
-failindex容许指定一个。变量,如果测试失败则设置这个变量,设置为字符串中第一个不能通过测试的字符的索引
% string is digit 1234
1
% string is digit a
0
% string is control ""
1
% string is control -strict ""
0
% string is digit -failindex idex "123c5"
0
% puts $idex
3
9.format创建字符串
format第一个参数是格式字符串,其中可以包含任意多个想%.3f这样的转换符。
% format "the square root of 10 is %.3f" [expr sqrt(10)]
the square root of 10 is 3.162
% set msg [format "%s is %d years old" "tom" 12]
tom is 12 years old
% for {set i 1} {$i<10} {incr i} {
puts [format "%4d %12.3f" $i [expr exp($i)]]
}
1 2.718
2 7.389
3 20.086
4 54.598
5 148.413
6 403.429
7 1096.633
8 2980.958
9 8103.084
%
10。scan是format的逆操作
scan获取的第一个参数是待解析的字符串,第二个蚕食是控制解析方式的格式字符串,其他参数是用来存储转换出的值,返回值2说明成功完成了2次转换
% scan "16 units,24.2% margin" "%d units, %f" a b
2
% set a
16
% set b
24.2
string match ?-nocase? pattern string
匹配返回1,否则返回0
% string match a* alp
1
% string match a* bat
0
% string match a alp
0
% string match {*\?} "Wow!
"
0
% string match {*\?} "what?"
1
11.正则表达式
\m,\M匹配一个单词的开头和结尾
\k匹配非字母非数字的字符k(如\.匹配字面上的句号)
{m}匹配与前述原子正好匹配m次的序列
{m,}匹配与前述原子至少匹配m次的序列
{m,n}匹配与前述原子至少匹配m次的序列,至多n次
re1|re2|re3...与任意一个指定正则表达式匹配
\v垂直制表符