# 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) ##### 測試 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}' ```
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. |