Powershell - Regular Expression - Match Character Classes


Advertisements

Following is the example of supported character classes in Windows PowerShell

##Format:  \p{name}
#Logic:   Matches any character in the named character class specified by
#         {name}. Supported names are Unicode groups and block ranges such
#         as Ll, Nd, Z, IsGreek, and IsBoxDrawing.
 "abcd defg" -match "\p{Ll}+"

#Format:  \P{name}
#Logic:   Matches text not included in the groups and block ranges specified
#         in {name}.
 1234 -match "\P{Ll}+"

#Format:  \w
#Logic:   Matches any word character. Equivalent to the Unicode character
#         categories [\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}\p{Pc}]. If ECMAScript-
#         compliant behavior is specified with the ECMAScript option, \w is
#         equivalent to [a-zA-Z_0-9].
 "abcd defg" -match "\w+" #(this matches abcd)

#Format:  \W
#Logic:   Matches any nonword character. Equivalent to the Unicode categories
#         [^\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}\p{Pc}].
 "abcd defg" -match "\W+" #(this matches the space)

#Format:  \s
#Logic:   Matches any white-space character. Equivalent to the Unicode
#         character categories [\f\n\r\t\v\x85\p{Z}].
 "abcd defg" -match "\s+"

#Format:  \S
#Logic:   Matches any non-white-space character. Equivalent to the Unicode
#         character categories [^\f\n\r\t\v\x85\p{Z}].
 "abcd defg" -match "\S+"

#Format:  \d
#Logic:   Matches any decimal digit. Equivalent to \p{Nd} for Unicode and
#         [0-9] for non-Unicode behavior.
 12345 -match "\d+"

#Format:  \D
#Logic:   Matches any nondigit. Equivalent to \P{Nd} for Unicode and [^0-9]
#         for non-Unicode behavior.
 "abcd" -match "\D+"

Output of all the above commands is True.

powershell_regex.htm
Advertisements