Anchors
An anchor does not match a character; instead, it matches what comes before (or after) a character:
Character | Description | Example |
^ | Beginning of a string | 'aaa' -match '^a' # True'bbb' -match '^a' # False |
$ | End of a string | 'ccc' -match 'c$' # True'ddd' -match 'c$' # False |
\b | Word boundary | 'Band and Land' -match '\band\b' # True'Band or Land' -match '\band\b' # False |
Anchors are useful where a character, string, or word may appear elsewhere in a string and the position is critical.For example, there might be a need to get values from the PATH environment variable that start with a specific drive letter. One approach to this problem is to use the start of a string anchor; in this case, retrieving everything that starts with the C drive:
$env:PATH -split ';' | Where-Object { $_ -match '^C' }
The example...