Regex basics
A regular expression is a pattern consisting of literal characters, an exact match, and special characters that have a variety of different behaviors. For example, the dot character, ".", matches any single character and not a literal dot.The -match
operator, which was introduced in Chapter 5, Operators can be used to test if a string matches a regular expression. The left hand side of the operator expects a string, the right a regular expression. For example:
'cat' -match '.*' # Match anything, returns true
'cat' -match '\d' # Match digits, returns false
The meaning of the string on the right in these examples is explored during this chapter.A few basic characters can go a long way. Several of the most widely used characters and operators introduced in this section are summarized in Table 9.1:
Character | Description | Example |
Any, except: [\^$.|?*+() | Literal character | 'a' -match 'a' # True'a&apos... |