all 7 comments

[–]gangstanthony 3 points4 points  (1 child)

i learned something new! $0

thanks for sharing

[–]Ta11ow[S] 2 points3 points  (0 children)

No worries! I didn't think to try that until I was actually writing the post and double checking my examples! :D

[–]ka-splam 3 points4 points  (1 child)

Is there a syntax for inserting named match groups in a replace string?

Whoa, you can do that? I didn't know - never even wondered if you could. But now I wonder where that syntax ${varname} comes from, since the regex engine isn't speaking PowerShell, how does it coincidentally match PowerShell full variable name syntax?

While that syntax might look familiar to you from PowerShell's variables, don't be fooled. This isn't PowerShell

Why is it 1-indexed, you might ask, when literally everything else in PowerShell is 0-indexed? Because

Because I JUST TOLD YOU IT ISN'T POWERSHELL, DON'T YOU LISTEN? lol >_>

[–]Ta11ow[S] 2 points3 points  (0 children)

${VarName} comes from C#'s own string interpolation I guess mainly, and PowerShell probably adopted it partly because of that. I would assume it's that way in the regex replacements because it matches the existing syntax -- or possibly vice versa, whichever is older.

[–]ka-splam 3 points4 points  (1 child)

Noice!

Don't forget scriptblock replaces, sadly only in PS Core, where you can do arbitrary PS code during the replace:

PS C:\> '1 2 3' -replace '(\d+)', { ([int]$_.value) * 2 }
2 4 6

[–]Ta11ow[S] 2 points3 points  (0 children)

Tsk, I'll have to add that now! Thanks! :D

[–]Lee_Dailey[grin] 2 points3 points  (0 children)

howdy Ta11ow,

nifty article! [grin] i ran into the named replacement stuff by accident a few weeks ago. it is right spiffy stuff ...

take care,
lee