tcl的正则表达式命令regexp的参数-lineanchor的理解
2013-11-20 18:52
429 查看
tcl中正则表达式用得很多,往往一句代码能够做好几条其他语句才能做完的工作,高效快捷;
在处理string中逐行匹配的情况下往往是用参数-line,但其实-line相当于-lineanchor和-linestop。
在使用-lineanchor时的实践:
-lineanchor
Changes the behavior of “^” and “$” (the “anchors”) so they match the beginning and end of a line respectively. This is the same as specifying the
(?w) embedded option (see the re_syntax manual page).
ex.
% set dataRtn {Select the version of EVDEV to use
> 1. Enable EVDEV 1.0.0.0 (EVDEV_1_0_0_0)
2. Disable EVDEV (NO_EVDEV)
choice[1-2]:
*
* EVDEV Menu
*
Select the version of EVDEV_DEMO to use
> 1. Enable EVDEV_DEMO 1.0.0.0 (EVDEV_1_0_0_0_DEMO_1_0_0_0)
2. Disable EVDEV_DEMO (NO_EVDEV_DEMO)
choice[1-2]:
Select the version of EVDEV_DRV to use
> 1. Enable EVDEV_DRV 1.0.0.0 (EVDEV_1_0_0_0_DRV_1_0_0_0)
2. Disable EVDEV_DRV (NO_EVDEV_DRV)
choice[1-2]:}
我想将上面变量dataRtn中的以*开头的所有行删除。
最开始认为参数-lineanchor会匹配到每行的开头和结尾,不会存在贪婪匹配的问题:
% regsub -all -lineanchor {^\*.*$} $dataRtn {} dataRtn1
% set dataRtn1
Select the version of EVDEV to use
> 1. Enable EVDEV 1.0.0.0 (EVDEV_1_0_0_0)
2. Disable EVDEV (NO_EVDEV)
choice[1-2]:
%
发现错了,虽然参数-lineanchor会让^和$ 匹配行首和行尾,但是照样是贪婪匹配。最后的$ 会一直匹配到整个string的结尾。
于是使用下面的命令:
% regsub -all -lineanchor {^\*.*?$} $dataRtn {} dataRtn1
% set dataRtn1
Select the version of EVDEV to use
> 1. Enable EVDEV 1.0.0.0 (EVDEV_1_0_0_0)
2. Disable EVDEV (NO_EVDEV)
choice[1-2]:
---------------------------->这里是空行
---------------------------->这里是空行
---------------------------->这里是空行
Select the version of EVDEV_DEMO to use
> 1. Enable EVDEV_DEMO 1.0.0.0 (EVDEV_1_0_0_0_DEMO_1_0_0_0)
2. Disable EVDEV_DEMO (NO_EVDEV_DEMO)
choice[1-2]:
Select the version of EVDEV_DRV to use
> 1. Enable EVDEV_DRV 1.0.0.0 (EVDEV_1_0_0_0_DRV_1_0_0_0)
2. Disable EVDEV_DRV (NO_EVDEV_DRV)
choice[1-2]:
期望把空行也删除。
% regsub -all -lineanchor {^\*.*?\n$} $dataRtn {} dataRtn1
% set dataRtn1
Select the version of EVDEV to use
> 1. Enable EVDEV 1.0.0.0 (EVDEV_1_0_0_0)
2. Disable EVDEV (NO_EVDEV)
choice[1-2]:
显然不行,虽然$会匹配到行尾,但是似乎回车换行(\n)不是每行的结尾,所以匹配失败,导致一直匹配到string的末尾;
% regsub -all -lineanchor {^\*.*?\n} $dataRtn {} dataRtn1
% set dataRtn1
Select the version of EVDEV to use
> 1. Enable EVDEV 1.0.0.0 (EVDEV_1_0_0_0)
2. Disable EVDEV (NO_EVDEV)
choice[1-2]:
Select the version of EVDEV_DEMO to use
> 1. Enable EVDEV_DEMO 1.0.0.0 (EVDEV_1_0_0_0_DEMO_1_0_0_0)
2. Disable EVDEV_DEMO (NO_EVDEV_DEMO)
choice[1-2]:
Select the version of EVDEV_DRV to use
> 1. Enable EVDEV_DRV 1.0.0.0 (EVDEV_1_0_0_0_DRV_1_0_0_0)
2. Disable EVDEV_DRV (NO_EVDEV_DRV)
choice[1-2]:
这就是想要的结果了——不把\n当作结尾。
在处理string中逐行匹配的情况下往往是用参数-line,但其实-line相当于-lineanchor和-linestop。
在使用-lineanchor时的实践:
-lineanchor
Changes the behavior of “^” and “$” (the “anchors”) so they match the beginning and end of a line respectively. This is the same as specifying the
(?w) embedded option (see the re_syntax manual page).
ex.
% set dataRtn {Select the version of EVDEV to use
> 1. Enable EVDEV 1.0.0.0 (EVDEV_1_0_0_0)
2. Disable EVDEV (NO_EVDEV)
choice[1-2]:
*
* EVDEV Menu
*
Select the version of EVDEV_DEMO to use
> 1. Enable EVDEV_DEMO 1.0.0.0 (EVDEV_1_0_0_0_DEMO_1_0_0_0)
2. Disable EVDEV_DEMO (NO_EVDEV_DEMO)
choice[1-2]:
Select the version of EVDEV_DRV to use
> 1. Enable EVDEV_DRV 1.0.0.0 (EVDEV_1_0_0_0_DRV_1_0_0_0)
2. Disable EVDEV_DRV (NO_EVDEV_DRV)
choice[1-2]:}
我想将上面变量dataRtn中的以*开头的所有行删除。
最开始认为参数-lineanchor会匹配到每行的开头和结尾,不会存在贪婪匹配的问题:
% regsub -all -lineanchor {^\*.*$} $dataRtn {} dataRtn1
% set dataRtn1
Select the version of EVDEV to use
> 1. Enable EVDEV 1.0.0.0 (EVDEV_1_0_0_0)
2. Disable EVDEV (NO_EVDEV)
choice[1-2]:
%
发现错了,虽然参数-lineanchor会让^和$ 匹配行首和行尾,但是照样是贪婪匹配。最后的$ 会一直匹配到整个string的结尾。
于是使用下面的命令:
% regsub -all -lineanchor {^\*.*?$} $dataRtn {} dataRtn1
% set dataRtn1
Select the version of EVDEV to use
> 1. Enable EVDEV 1.0.0.0 (EVDEV_1_0_0_0)
2. Disable EVDEV (NO_EVDEV)
choice[1-2]:
---------------------------->这里是空行
---------------------------->这里是空行
---------------------------->这里是空行
Select the version of EVDEV_DEMO to use
> 1. Enable EVDEV_DEMO 1.0.0.0 (EVDEV_1_0_0_0_DEMO_1_0_0_0)
2. Disable EVDEV_DEMO (NO_EVDEV_DEMO)
choice[1-2]:
Select the version of EVDEV_DRV to use
> 1. Enable EVDEV_DRV 1.0.0.0 (EVDEV_1_0_0_0_DRV_1_0_0_0)
2. Disable EVDEV_DRV (NO_EVDEV_DRV)
choice[1-2]:
期望把空行也删除。
% regsub -all -lineanchor {^\*.*?\n$} $dataRtn {} dataRtn1
% set dataRtn1
Select the version of EVDEV to use
> 1. Enable EVDEV 1.0.0.0 (EVDEV_1_0_0_0)
2. Disable EVDEV (NO_EVDEV)
choice[1-2]:
显然不行,虽然$会匹配到行尾,但是似乎回车换行(\n)不是每行的结尾,所以匹配失败,导致一直匹配到string的末尾;
% regsub -all -lineanchor {^\*.*?\n} $dataRtn {} dataRtn1
% set dataRtn1
Select the version of EVDEV to use
> 1. Enable EVDEV 1.0.0.0 (EVDEV_1_0_0_0)
2. Disable EVDEV (NO_EVDEV)
choice[1-2]:
Select the version of EVDEV_DEMO to use
> 1. Enable EVDEV_DEMO 1.0.0.0 (EVDEV_1_0_0_0_DEMO_1_0_0_0)
2. Disable EVDEV_DEMO (NO_EVDEV_DEMO)
choice[1-2]:
Select the version of EVDEV_DRV to use
> 1. Enable EVDEV_DRV 1.0.0.0 (EVDEV_1_0_0_0_DRV_1_0_0_0)
2. Disable EVDEV_DRV (NO_EVDEV_DRV)
choice[1-2]:
这就是想要的结果了——不把\n当作结尾。
相关文章推荐
- tcl 正则表达式命令regexp 参数-inline的理解
- javascript正则表达式多行匹配和RegExp理解
- 常用命令及参数正则表达式总结
- RegExp 对象(正则表达式)
- Linux常用命令―grep及正则表达式
- sys_connect_by_path+regexp_substr树状结构,并用正则表达式拆分
- Javascript url地址传参及获取参数方法(利用正则表达式)
- 深入理解正则表达式环视的概念与用法
- JavaScript RegExp 正则表达式对象详细说明
- 简单又复杂的正则表达式RegExp
- [原创] jQuery源码分析-02正则表达式-RegExp-常用正则表达
- 关于正则表达式中的(?=re)的理解和例子
- 深入理解-Js正则表达式-1
- 基于Linux下详解正则表达式(基本正则和扩展正则命令使用实例)
- MySQL中REGEXP正则表达式使用大全
- 理解 Java 正则表达式怪异的 \\ 和 \\\\,让您见怪不怪
- linux学习之grep egrep 命令及正则表达式
- 快速掌握grep命令及正则表达式
- 用一个实例讲解rename命令中正则表达式的使用
- urlrewrite伪静态 及多参数传递-附正则表达式语法 [轉]