shell高階 正規表示式

2022-01-29 19:36:32 字數 3804 閱讀 9297

正規表示式是一種定義的規則,linux工具可以用它來過濾文字。

純文字

[root@node1 ~]# echo "this is a cat" | sed -n '/cat/p'

this is a cat

[root@node1 ~]# echo "this is a cat" | gawk '/cat/'

this is a cat

正規表示式的匹配非常挑剔,尤其需要記住,正規表示式區分大小寫。

特殊字元

正規表示式識別的特殊字元包括:

如果要使用某個特殊字元作為文字字元,就必須轉義,一般用(\)來轉義。

[root@node1 ~]# echo "this is  a $" | sed -n '/\$/p'

this is a $

錨字元有兩個特殊字元可以用來將模式鎖定在資料流的行首或行尾

脫字元(^)定義從資料流中文本行的行首開始的模式。

美元符($)定義了行尾錨點。

[root@node1 ~]# echo "this is a cat" | sed -n '/^this/p'

this is a cat

[root@node1 ~]# echo "this is a cat" | sed -n '/cat$/p'

this is a cat

在一些情況下可以組合使用這兩個命令

1.比如查詢只含有特定文字的行

[root@node1 ljy]# more test.txt            

this is a dog

what

howthis is a cat

is a dog

[root@node1 ljy]# sed -n '/^is a dog$/p' test.txt

is a dog

[root@node

2.兩個錨點組合起來,可以直接過濾空白行

[root@node1 ljy]# more test.txt           

this is a dog

what

howthis is a cat

is a dog

[root@node1 ljy]# sed '/^$/d' test.txt

this is a dog

what

howthis is a cat

is a dog

點號字元

點號用來匹配除換行符外的任意單個字元,他必須匹配乙個字元。

[root@node1 ljy]# more test.txt 

this is a dog

what

howthis is a cat

is a dog

at[root@node1 ljy]# sed -n '/.at/p' test.txt

what

this is a cat

字元組限定待匹配的具體字元,使用字元組。使用方括號來定義乙個字元組。

[root@node1 ljy]# more test.txt 

this is a dog

this is a dog

this is a dog

this is a cat

[root@node1 ljy]# sed -n '/[dd]og/p' test.txt

this is a dog

this is a dog

[root@node1 ljy]# sed -n '/[dd]o[gg]/p' test.txt

this is a dog

this is a dog

this is a dog

排除型字元組

要排除某些特定的元素,要在字元組前面加個脫字元。

[root@node1 ljy]# sed -n '/[dd]o[gg]/p' test.txt  

this is a dog

this is a dog

this is a dog

[root@node1 ljy]# sed -n '/[^d]og/p' test.txt

this is a dog

區間正規表示式會包括此區間內的任意字元。

[root@node1 ljy]# more test.txt 

123123

1231

121222222

412345341613

vsdvs

qwer12344123

12345

34211

444444

[root@node1 ljy]# sed -n '/^[0-9][0-9][0-9][0-9][0-9]$/p' test.txt

12345

34211

問號問號表明前面的字元出現0次或者1次,僅限於此。

[root@node1 ljy]# echo "bat" | gawk '/ba?t/'  

bat[root@node1 ljy]# echo "baat" | gawk '/ba?t/'

[root@node1 ljy]# echo "bt" | gawk '/ba?t/'

bt

可以將問號和字元組一起使用

[root@node1 ljy]# echo "bt" | gawk '/b[ae]?t/' 

bt[root@node1 ljy]# echo "bat" | gawk '/b[ae]?t/'

bat[root@node1 ljy]# echo "bet" | gawk '/b[ae]?t/'

bet[root@node1 ljy]# echo "baat" | gawk '/b[ae]?t/'

加號加號表明前面的字元可以出現一次或多次,但至少是1次。

[root@node1 ljy]# echo "baat" | gawk '/b[ae]+t/' 

baat

[root@node1 ljy]# echo "bt" | gawk '/b[ae]+t/'

[root@node1 ljy]# echo "bt" | gawk '/ba+t/'

[root@node1 ljy]# echo "bat" | gawk '/ba+t/'

bat[root@node1 ljy]# echo "baat" | gawk '/ba+t/'

baat

花括號ere中的花括號允許你為可重複的正規表示式規定上下限。

m,n最少出現m此,最多出現n次。

[root@node1 ljy]# echo "baat" | gawk '/b[ae]t/'  

baat

[root@node1 ljy]# echo "baaat" | gawk '/b[ae]t/'

管道符號

用邏輯or的方式指定正規表示式規則,其中乙個條件符合要就即可。

表示式分組

正規表示式分組也可以用圓括號進行分組。

[root@node1 ljy]# echo "bat" | gawk '/b(a|e)t/'           

bat[root@node1 ljy]# echo "baat" | gawk '/b(a|e)t/'

[root@node1 ljy]# echo "bet" | gawk '/b(a|e)t/'

bet

JavaScript高階 正規表示式

前言 這篇部落格是我對正規表示式一些基礎知識的整理。基礎部分 1.1 什麼是正規表示式 是一種特殊的字串模式 作用是匹配字串 如同用模具做產品,正規表示式就是模具,具體的字串就是產品 定義一種規則去匹配復合規則的字串 1.2 元字元介紹 會匹配行或者字串的開頭,有時會匹配整個文件的起始位置 匹配字串...

Python 高階 正規表示式1

匯入re模組 import re 使用match方法進行匹配操作 result re.match 正規表示式,要匹配的字串 如果上一步匹配到資料的話,可以使用group方法來提取資料 滿足規範要求的資料 result.group 1.檢視乙個字串是否符合python變數規範 import re 判斷...

JS基礎高階 正規表示式

1 regexpbuddy 正規表示式中有些字元有特殊的語法含義,是不能直接使用的,必須使用 進行轉義後才能使用。正規表示式使用如下語法匹配乙個範圍內的字元 語法 含義 示例 abc 匹配集合內的任意字元 ac abc 匹配不在字符集內的任意字元 ac 0 9 匹配任意乙個數字 a z 匹配任意乙個...