Regex matches in PowerShell
How to find regex matches using PowerShell
-match
operator
In PowerShell the -match
operator will evaluate whether a string matches a regular expression:
|
|
When you use the -match
operator the variable $Matches
is populated with all the matches:
|
|
In particular if you used named matches you can retrieve the matched value by accessing a property on the $Matches
variable with the same name:
|
|
Equivalent of grep
in PowerShell
To find regular expression matches in files in the current directory use the Select-String
commandlet:
|
|
To search for a regular expression in files in the current directory and all subdirectories:
|
|
Finding all matches
Use the -AllMatches
switch on Select-String
to find all pattern matches rather than just the first:
|
|
History of grep
Here’s an interesting video with Brian Kernighan about the history of grep.
g/re/p - global / regular expression / print