正则表达式学习-TCL

前端之家收集整理的这篇文章主要介绍了正则表达式学习-TCL前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

#a 的3、4之间有3个空格

% set a "1 a 3 4 5
aaa xx11 22 3"
1 a 3 4 5
aaa xx11 22 3
#把a用空格分开

% split $a
1 a 3 {} {} 4 5 aaa xx11 22 3
% set num [lindex [split $a] 2]
3
% set num [lindex [split $a] 3]
% set num [lindex [split $a] 4]
% set num [lindex [split $a] 5]
4
% set num [lindex [split $a] 6]
5
% set num [lindex [split $a] 7]
aaa
%

###########

% regexp ^a b
0
% regexp ^a abc
1
% regexp $a abc
0
% regexp $a abca
0
% regexp $a abca
0
% regexp a$ abca
1
% regexp a$ abcad
0
% regexp ad$ abcad
1
% regexp ae$ abcad
0
% regexp \ma abcad
0
% regexp \mabcad\M abcad
0

% regexp ? abcad
couldn't compile regular expression pattern: quan
% regexp [?] abcad
invalid command name "?"
%
% regexp [1-9] abcad
invalid command name "1-9"
% regexp ([1-9]} abcad
invalid command name "1-9"
[ ]用法

% regexp {[1-9]} abcad
0
% regexp {[1-9]} 3
1
% regexp {[1-9]} 8
1
% regexp {[a-e]} a
1

% regexp {[a-e]} f
0

用法
% regexp {[a?]} a
1
% regexp {[a?]} b
0
% regexp {a?} b
1
% regexp {a?} bc
1

+用法
% regexp {[1-3]+} bc
0
% regexp {[1-3]+} 2
1
% regexp {[1-3]+} 22
1
% regexp {[1-3]+} 223
1
% regexp {[1-3]+} 2234
1
% regexp {[1-3]+} 2234
1
% regexp {[1-3]+} 4
0

资料例子

% regexp {^[0-9]+$} 510
1
% regexp {^[0-9]+$} -510
0
% regexp {([0-9]+) *([a-z]+)} "Walk 10 km"
1

-inline 选项让regexp把匹配变量返回1个数据列表。
% regexp -inline {([0-9]+) *([a-z]+)} "Walk 10 km"
{10 km} 10 km
% regexp -inline {([0-9]+) *([a-z]+)} "Walk 10 km" a b c
regexp match variables not allowed when using -inline
% regexp {([0-9]+) *([a-z]+)} "Walk 10 km" a b c
1
% puts "$a,$b,$c"
10 km,10,km

-all

Causes the regular expression to be matched as many times as possible in the string,returning the total number of matches found. If this is specified with match variables,they will contain information for the last match only. % regexp -all {[0-7]} wrong # args: should be "regexp ?switches? exp string ?ma bMatchVar ...?" % set string abc123a abc123a % regexp -all {[0-7]} $string 3 % set string2 ab3c123a ab3c123a % regexp -all {[0-7]} $string2 4

原文链接:https://www.f2er.com/regex/362428.html

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