Skip to main content

Regex 正規表示式

教學網站

測試 Regex 語法 (awk)
# echo "Testing regex using awk" | awk '/regex/{print $0}'
Testing regex using awk

# echo "\ is a special character" | awk '/\\/{print $0}'
\ is a special character

# echo "likegeeks website" | awk '/^likegeeks/{print $0}'
likegeeks website
基本搜尋
# 搜尋字串區分大小寫
echo "This is a test" | sed -n '/test/p'
echo "This is a test" | awk '/test/{print $0}'

# 可以包含空白與數字
echo "This is a test 2 again" | awk '/test 2/{print $0}' 
Operators
Description
[abc]
Match any single character from from the listed characters
[a-z]
Match any single character from the range of characters
[^abc]
Match any single character not among listed characters
[^a-z]
Match any single character not among listed range of characters
.
Match any single character except a newline
\
Turn off (escape) the special meaning of a metacharacter
^
Match the beginning of a line.
$
Match the end of a line.
*
Match zero or more instances of the preceding character or regex.
?
Match zero or one instance of the preceding character or regex.
+
Match one or more instances of the preceding character or regex.
{n,m} Match a range of occurrences (at least n, no more than m) of preceding character of regex.
|
Match the character or expression to the left or right of the vertical bar.
數字字元
  • [[:digit:]]* : any number of digits (zero or more)
  • [[:digit:]]+ : at least one digit
  • [[:digit:]]? : zero or one digits
  • [[:digit:]]{1,3} : at least one and no more than three digits
  • [[:digit:]]{2,} : two or more digits
  • [[:digit:]]{3} : three digits
特殊字元

字元:.*[]^${}\+?|()

$ cat myfile
There is 10$ on my pocket

awk '/\$/{print $0}' myfile

echo "\ is a special character" | awk '/\\/{print $0}' 

echo "This ^ is a test" | sed -n '/s ^/p'
搜尋字首與字尾
# 搜尋字首
$ echo "likegeeks website" | awk '/^likegeeks/{print $0}'

# 搜尋字尾
$ echo "This is a test" | awk '/test$/{print $0}' 

# 搜尋相同字元行的文字
$ awk '/^this is a test$/{print $0}' myfile

# 不顯示空白行
$ awk '!/^$/{print $0}' myfile
搜尋字串

用 dot

# cat myfile
this is a test
This is another test
And this is one more
start with this

# awk '/.st/{print $0}' myfile
this is a test
This is another test

用 [ ]

// 包含 oth 與 ith
$ awk '/[oi]th/{print $0}' myfile
This is another test
start with this

// 不包含 oth 與 ith
$ awk '/[^oi]th/{print $0}' myfile
And this is one more
start with this                  <== 有兩個 th

使用範圍

$ awk '/[e-p]st/{print $0}' myfile
this is a test
This is another test

$ echo "123" | awk '/[0-9][0-9][0-9]/'

$ awk '/[a-fm-z]st/{print $0}' myfile
this is a test
This is another test