tcl 正则表达式命令regexp 参数-inline的理解

tcl “似乎”在8.4以后的版本对正则表达式命令regexp 提供了一个新的参数 -inline:

-inline
Causes the command to return, as a list, the data that would otherwise be placed in match variables. When using -inline, match variables may not be specified. If used with -all, the list will be concatenated at each iteration, such that a flat list is always returned. For each match iteration, the command will append the overall match data, plus one element for each subexpression in the regular expression. Examples are:
regexp -inline -- {\w(\w)} " inlined "
       in n
regexp -all -inline -- {\w(\w)} " inlined "
       in n li i ne e

理解为表达式的返回值不再是1(成功)或者0(失败)。

而是一个List,这个list 的value是 正则表达式的pattern 所匹配的所有内容+各个子模式匹配的内容(每个子模式匹配结果是list的一个element);

如果加了-all 的参数,那么返回的List就是所有迭代匹配后的结果连接(contat)成为的内容。


其实如果不加-all的话,其等价于:

regexp -- {\w(\w)} "  inlined  " match sub1

其中match是所有的匹配内容,而sub1是子模式内容。


实验时遇到一个问题(其实跟-inline没关系):

% set tokenList_02 {vsb_components_t1 = "FEATURE_IPNET_INET6 2 FEATURE_SNMP_V3 y COMPONENT_TUNNEL_SUPPORT y SNMP[0-9_]+ 1 FEATURE_SNMP_AGENTX y "}


% set token_items [regexp -inline {([^\s\=\"]+)\s*=\s*(.*)} $tokenList_02]

% lindex $token_items 0
vsb_components_t1 = "FEATURE_IPNET_INET6 2 FEATURE_SNMP_V3 y COMPONENT_TUNNEL_SUPPORT y SNMP[0-9_]+ 1 FEATURE_SNMP_AGENTX y "

% lindex $token_items 1
vsb_components_t1

% lindex $token_items 2
"FEATURE_IPNET_INET6 2 FEATURE_SNMP_V3 y COMPONENT_TUNNEL_SUPPORT y SNMP[0-9_]+ 1 FEATURE_SNMP_AGENTX y "

% llength [lindex $token_items 2]
1                         ------------------------------------------------->怎么可能是1呢,不该是10吗?


实际上没有看到那双引号,这双引号让所有的内容都是list的一个element。

% llength [lindex [lindex $token_items 2] 0]
10


这才是想要的结果。


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值