Skip to content

Instantly share code, notes, and snippets.

@jcdickinson
Created August 18, 2016 18:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcdickinson/cee4582448300c0d404bbff1f5669029 to your computer and use it in GitHub Desktop.
Save jcdickinson/cee4582448300c0d404bbff1f5669029 to your computer and use it in GitHub Desktop.
Select Regex
Function Select-Regex {
Param(
[Parameter(ValueFromPipeline=$True)] $Input,
[Parameter(Mandatory=$True, Position=0)] [String] $Pattern
)
Begin {
$Regex = New-Object Regex $Pattern, 'ExplicitCapture'
}
Process {
$Input | ForEach-Object { $Regex.Matches($_) } | Where-Object { $_.Success } | ForEach-Object {
$Result = @{}
ForEach ($Group in $Regex.GetGroupNames())
{
Add-Member -InputObject $Result -MemberType NoteProperty -Name $Group -Value ($_.Groups[$Group].Value)
}
$Result
}
}
}
Get-ChildItem *.js | Get-Content | Select-Regex 'require\("(?<Require>.*?)"\)' | Select-Object -ExpandProperty Require
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment