linux正则表达式(一)

  正则表达式是一种字符串模式匹配,使用灵活、功能强大,使用简单的方式对字符串进行控制。

1.使用grep进行字符串匹配

测试文本 1.txt

helloworld
haa
12345678
!@#$%^&*(
ASDwexvRSD
,./]];]{}_()^%
asdasdasd

 搜索文件中包含"hello"

grep -n 'hello' 1.txt

  利用中括号 [] 来搜寻集合字符

$ grep -n "h[ae]" 1.txt
1:helloworld
2:haa

行首与行尾字符 ^ $

$ grep -n "^he" 1.txt
1:helloworld
$ grep -n '%$' 1.txt
6:,./]];]{}_()^%

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

$ grep -n 'h.' 1.txt
1:helloworld
2:haa
$ grep -n "he.*ld" 1.txt
1:helloworld

限定连续 RE 字符范围 {}

$ grep -n 'ha{2}' 1.txt
2:haa
原文地址:https://www.cnblogs.com/lzeffort/p/7231884.html