![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Use of matching subexpressions in "-replace" operator Someone, please clarify if so-called "matching subexpressions" can be used in the replacement string of the PowerShell "-replace" operator? E.g. something like "20070909" -replace "(\d\d\d\d)(\d\d)(\d\d)","$1-$2-$3" to yield "2007-09-09"? The above statement actually returns "--" in PowerShell, since $1, $2, and $3 apparently are undefined. I didn't expect this behaviour since "matching subexpressions" are defined in .NET regular expressions and they seem to be allowed in the search string of the -replace operator.. Audun. |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Use of matching subexpressions in "-replace" operator Yep "20070909" -replace "(\d\d\d\d)(\d\d)(\d\d)",'$1-$2-$3' 2007-09-09 Shay http://scriptolog.blogspot.com Quote: > Someone, please clarify if so-called "matching subexpressions" can be > used in the replacement string of the PowerShell "-replace" operator? > E.g. something like > > "20070909" -replace "(\d\d\d\d)(\d\d)(\d\d)","$1-$2-$3" > > to yield "2007-09-09"? > > The above statement actually returns "--" in PowerShell, since $1, $2, > and $3 apparently are undefined. I didn't expect this behaviour since > "matching subexpressions" are defined in .NET regular expressions and > they seem to be allowed in the search string of the -replace > operator.. > > Audun. > |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Use of matching subexpressions in "-replace" operator Use single quotes to avoid variable expansion: "20070909" -replace "(\d\d\d\d)(\d\d)(\d\d)", '$1-$2-$3' -- Kiron |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Use of matching subexpressions in "-replace" operator Thank you! Quite subtle error. The devil surely is in the details... Audun "Kiron" <Kiron@xxxxxx> wrote in message news:umzZ%23Lx8HHA.5424@xxxxxx Use single quotes to avoid variable expansion: "20070909" -replace "(\d\d\d\d)(\d\d)(\d\d)", '$1-$2-$3' -- Kiron |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| A "global" FIND & REPLACE on the Registry? | Vista General | |||
| Need to replace files "searchfilterhost.exe" | Vista General | |||
| Turn off "replace wireless mouse batter" alert | Vista performance & maintenance | |||
| Need some "enlightenment" on invoke-operator | PowerShell | |||
| Regex for matching "<a@c.d>" | PowerShell | |||