tcl-regular expression-practice

% set text "Some arbitrary text which might include \$ or {"
Some arbitrary text which might include $ or {
% regexp -inline -all -- {\S+} $text
Some arbitrary text which might include {$} or \{
% set str 66.70.7.154
66.70.7.154
% regexp "(\[0-9]{1,3})\.(\[0-9]{1,3})\.(\[0-9]{1,3})\.(\[0-9]{1,3})" $str all f
irst second third fourth
1
%  puts "$all \n $first \n $second \n $third \n $fourth \n"
66.70.7.154
 66
 70
 7
 154

%  puts "$all\n$first\n$second\n$third\n$fourth\n"
66.70.7.154
66
70
7
154

% set string "0377.255.255.255"
0377.255.255.255
% regexp {^\d+\.\d+\.\d+\.\d+$} $string
1
% regexp {^\d+\.\d+\.\d+\.\d+$} $string a b c d
1
% puts "$a,$b,$c,$d"
0377.255.255.255,,,
% puts "$a"
0377.255.255.255

% set word "foo"
foo
% regexp {(foo|bar)} match zzz
0
% regexp {(foo|bar)} match foo
0
% regexp {(foo|bar)}  foo
1
% set word "foo"
foo
% set result [regexp {(foo|bar)} $word match zzz]
1
% set match
foo
% set zzz
foo

% set result [regexp {(foo|bar)} $word match]
1
% set match
foo

% set str "  sjkhf sdhj   "
  sjkhf sdhj
% regexp {^ +.* +$} $str match
1
% set match
  sjkhf sdhj
% set rest [regexp {^ +.* +$} $str match]
1

% regexp {abc$} dabc
1
% regexp {abc$} dabcd
0
% regexp {\d} dabcd
0
% regexp {\d} 123
1
% regexp {\d} "123 abc"
1
% regexp {\d} "123 abc" num
1
% set num
1
% regexp {\d} "323 abc" num
1
% set num
3
% regexp {\d+} "123 abc" num
1
% set num
123
% regexp {\d+} "323d 33 abc" num
1
% set num
323
% regexp {\w+} "123 abc" num
1
% set num
123
% regexp {\w+} "a1234 abc" num
1
% set num
a1234
%
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值