Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell Documentation for RegEx language

Where in the PowerShell docs does it talk about using the special variables $1, $2, etc. to represent captures in a -replace regex?

'asdf' -replace '(a)(\w{3})', '$2$1';

# Result: sdfa

It doesn't seem to appear in about_Comparison_Operators, where I'd expect it to, along with the -replace operator. I also looked in about_Regular_Expressions, and it doesn't appear in there.

I did find an example at this blog post, but can't seem to locate anything in the official docs.

http://windows-powershell-scripts.blogspot.com/2009/06/regular-expressions-in-powershell.html


1 Answers

Powershell is based on the .Net framework, so Powershell uses the same regex-"engine". For documentation on the regex language .Net uses, check out:

Regular Expression Language - Quick Reference

$(number) are characters used for Substitutions

Btw, if you had read the whole Get-help about_Regular_Expressions (especially the last sentences), you would have seen:

For more information, see the "Regular Expression Language Elements" topic in the Microsoft Developer Network (MSDN) library at http://go.microsoft.com/fwlink/?LinkId=133231.

like image 161
Frode F. Avatar answered Aug 17 '26 12:08

Frode F.