In PowerShell I find myself doing this kind of thing over and over again for matches:
some-command | select-string \'^(//[^#]*)\' |
%{some-other-command
You can use the -match
operator to reformulate your command as:
some-command | Foreach-Object { if($_ -match '^(//[^#]*)') { some-other-command $($matches[1])}}
You could try this:
Get-Content foo.txt | foreach { some-othercommand [regex]::match($_,'^(//[^#]*)').value }
Named Replacement
'foo bar' -replace '(?<First>foo).+', '${First}'
Returns: foo
Unnamed Replacement
'foo bar' -replace '(foo).+(ar)', '$2 z$2 $1'
Returns: ar zar foo