Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Use of matching subexpressions in "-replace" operator

Reply
 
Old 09-09-2007   #1 (permalink)
Audun Gjerken


 
 

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 SpecsSystem Spec
Old 09-09-2007   #2 (permalink)
Shay Levi


 
 

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 SpecsSystem Spec
Old 09-09-2007   #3 (permalink)
Kiron


 
 

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 SpecsSystem Spec
Old 09-09-2007   #4 (permalink)
Audun Gjerken


 
 

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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46