正则表达式

来源:互联网 发布:ubuntu命令行修改文件 编辑:程序博客网 时间:2024/06/03 02:27

1.行首与行尾字符^$

行首:

oyzhx@ubuntu:~$ grep -n '^the' regular_express.txt 12:the symbol '*' is represented as start.   
行尾

oyzhx@ubuntu:~$ grep -n '\.$' regular_express.txt 1:"Open Source" is a good mechanism to develop programs.

2.任意个字符.与重复字符*

*:重复前一个字符的0-无穷个的意思.//a*

oyzhx@ubuntu:~$ grep -n 'ooo*' regular_express.txt 1:"Open Source" is a good mechanism to develop programs.2:apple is my favorite food.3:Football game is not use feet only.9:Oh! The soup taste good.18:google is the best tools for search keyword.19:goooooogle yes!
.:代表一定有一个任意字符的意思.

oyzhx@ubuntu:~$ grep -n 'g..d' regular_express.txt 1:"Open Source" is a good mechanism to develop programs.9:Oh! The soup taste good.16:The world <Happy> is the same with "glad".
".*"代表零个或多个任意字符

oyzhx@ubuntu:~$ grep -n 'g.*g' regular_express.txt 1:"Open Source" is a good mechanism to develop programs.14:The gd software is a library for drafting programs.18:google is the best tools for search keyword.19:goooooogle yes!20:go! go! Let's go.

3.限定连续RE字符范围{}

{2}表示2个,{2,}2个以上,{2,5}2个到5个

oyzhx@ubuntu:~$ grep -n 'go\{2,5\}g' regular_express.txt 18:google is the best tools for search keyword.
4.sed工具

sed [-nefr]

-n:显示n特殊处理的那行

-e:命令行模式上进行sed的动作编辑

-f:将sed的动作写在一个文件内

-r:扩展型正则表达式的语法(默认是基础正则表达式语法)

-i:直接修改读取的文件内容,而不是由屏幕输出

[n1,[n2]] function

function 的参数

a,新增;c替换 d删除,i插入;p打印,s替换

5.扩展正则表达式

+:重复一个或一个以上的前一个RE字符. egrep -n 'go+d' aa.txt;

?:零个或一个的前一个RE字符  egrep -n 'go?d' aa.txt;

|:用或(or)的方式找出数个字符串 egrep -n 'gdlggod' aa.txt;

():找出“组字符串” egrep -n 'g(laloo)d' aa.txt;

()+:多个重复组的判别  echo 'AxyzxyzxuzusC'|egrep 'A(xyz) +C';

0 0
原创粉丝点击