Skip to content

Instantly share code, notes, and snippets.

@turboBasic
Last active May 28, 2018 19:40
Show Gist options
  • Save turboBasic/42f6d9dfed59205c8dbdce93bcb41986 to your computer and use it in GitHub Desktop.
Save turboBasic/42f6d9dfed59205c8dbdce93bcb41986 to your computer and use it in GitHub Desktop.
[Get-RegexMatchExamples] Using regex matches in powershell. Get-Matches() gets all regex matches. Answers question "How do I iterate through text file and print all matched regex groups for each line in file?" #powershell #regex
Get-Content SomeFile.ps1 |
ForEach { Select-String '(?ix) ^ \s* function \s+ ( [^{( ]+ ) \s* ( \( [^()]+ \) )? \{' -input $_ -Allmatch } |
ForEach { $_.Matches.Groups[1].Value }
Get-Content SomeFile.ps1 | Select-String '(?i)^function\s+((\w|-)+)' | ForEach { $_.Matches.Groups[1].Value }
Get-Content SomeFile.ps1 |
ForEach-Object { $_ -match '(?ix) ^ \s* function \s+ ( [^{( ]+ ) \s* ( \( [^()]+ \) )? \{' } |
Where-Object { $_ } |
ForEach-Object { $Matches[1] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment