本文介紹PowerShell中的正則表達式,各種不同的字符代表不同的含義,包括占位符PlaceHolder、量詞Quantifier和邊界字符。
下面列舉PowerShell的正則表達式中可能出現的字符,以及它們表示的含義。
字符串的匹配符(占位符PlaceHolder)
. 這是一個點兒,表示換行符之外的任意一個字符(Any character except newline (Equivalent: [^\n]))
[^abc] 指定的字符(abc)之外的任意一個字符,可以把abc換成其它字符組。(All characters except the ones specified)
[^a-z] 任意一個非小寫字母的字符(All characters except those in the region specified)
[abc] 指定的字符集中的任意一個,即abc中的任意一個(One of the characters)
[a-z] 指定的字符范圍中的任意一個,即任意一個小寫字母。One of the characters in the region
\a 響呤(Bell (ASCII 7))
\c Any character allowed in XML names
\cA-\cZ Control+A to Control+Z, ASCII 1 to ASCII 26
\d 任意一個數字,等同于[0-9](Any number (Equivalent: [0-9]))
\D 任意一個非數字。Any non-number
\e ESC鍵(Escape (ASCII 27))
\f Form Feed, (ASCII 12)
\n 換行Line break
\r 回車Carriage return
\s 任意一個空白鍵(空白鍵如tab,換行)Any whitespace (space, tab, new line)
\S 任意一個非空白字符(Any non-whitespace)
\t tab鍵
\w 字母,數字和下劃線(Letter, number or underline)
\W \w的補集(Non-letter, number, or underline)
匹配次數(量詞Quantifier)
* 出現零次、1次、多次(Any (no occurrence, once, many times))
? 出現零次、1次(No occurrence or one occurrence)
{n,} 出現至少n次(At least n occurrences)
{n,m} 出現至少n次,最多m次(At least n occurrences, maximum m occurrences)
{n} 出現n次(Exactly n occurrences)
+ 出現1次、多次(One or many occurrences)
所有的匹配次數的符號,默認情況下都是貪婪的,即它將最大長度的進行匹配。如果想要得到最短的匹配,那就要在上面這組符號之后加一個問號(?)。
匹配邊界
$ 字符串結束(End of text)
^ 字符串開始(Start of text)
\b Word boundary
\B No word boundary
\G After last match (no overlaps)
關于PowerShell正則表達式參考,本文就介紹這么多,希望對您有所幫助,謝謝!
您可能感興趣的文章:- Shell正則表達式之grep、sed、awk實操筆記
- shell 正則表達式詳細整理
- Shell if中的正則表達式使用詳解
- Shell正則表達式學習筆記
- PowerShell中正則表達式使用例子
- PowerShell中使用正則表達式匹配字符串實例
- shell腳本之正則表達式、grep、sed、awk