# Regex 正規表示式

教學網站

- [https://likegeeks.com/regex-tutorial-linux/](https://likegeeks.com/regex-tutorial-linux/)
- [http://overapi.com/regex](http://overapi.com/regex)
- [https://regexper.com/#%2F.echo%24%2F](https://regexper.com/#%2F.echo%24%2F) (視覺化正規表示式)
- [The Greatest Regex Trick Ever](http://rexegg.com/regex-best-trick.html)
- [How to Get Started With Regular Expressions in the Linux Terminal](https://www.howtogeek.com/get-started-with-regex-in-linux-terminal/)
- [Regex Vis](https://regex-vis.com/samples)

##### 測試 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
```

##### 基本搜尋

```editable
# 搜尋字串區分大小寫
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}' 
```

<table border="1" id="bkmrk-operators-descriptio" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 26.3426%;"></col><col style="width: 73.75%;"></col></colgroup><tbody><tr><td>Operators  
</td><td>Description  
</td></tr><tr><td>\[abc\]  
</td><td>Match any single character from from the listed characters</td></tr><tr><td>\[a-z\]  
</td><td>Match any single character from the range of characters</td></tr><tr><td>\[^abc\]  
</td><td>Match any single character not among listed characters</td></tr><tr><td>\[^a-z\]  
</td><td>Match any single character not among listed range of characters</td></tr><tr><td>.  
</td><td>Match any single character except a newline</td></tr><tr><td>\\  
</td><td>Turn off (escape) the special meaning of a metacharacter</td></tr><tr><td>^  
</td><td>Match the beginning of a line.</td></tr><tr><td>$  
</td><td>Match the end of a line.</td></tr><tr><td>\*  
</td><td>Match zero or more instances of the preceding character or regex.</td></tr><tr><td>?  
</td><td>Match zero or one instance of the preceding character or regex.</td></tr><tr><td>+  
</td><td>Match one or more instances of the preceding character or regex.</td></tr><tr><td>{n,m}</td><td>Match a range of occurrences (at least n, no more than m) of preceding character of regex.</td></tr><tr><td>|  
</td><td>Match the character or expression to the left or right of the vertical bar.</td></tr></tbody></table>

##### 數字字元

- `[[: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

##### 特殊字元

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

```editable
$ 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'
```

##### 搜尋字首與字尾

```editable
# 搜尋字首
$ 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

```editable
# 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 
```

使用 ? : 特定單一字元出現 0 ~ 1 次

```bash
grep -E "^#?DS" /etc/mail/sendmail.cf

# Return:
# DS
# #DS[192.168.21.75]
# #DS[10.14.202.142]
```