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 Tutorial - -replace operator

Reply
 
Old 06-30-2006   #1 (permalink)
Bob Weiner
Guest


 
 

-replace operator

This is probably a regex question as opposed to powershell but I am confused
by an example of the -replace operator on page 89 in the user guide:

> "abc" -replace "\w*", "new"

newnew

Why does this match twice? It seems to me that if \w* is greedy, it should
take them all. If it is not, it should grab "", "a", "ab", "abc".

The
> "abc" -replace "\w?", "new"

example is similar.

bob


My System SpecsSystem Spec
Old 06-30-2006   #2 (permalink)
Andrew Watt [MVP]
Guest


 
 

Re: -replace operator

Bob,

My interpretation is that the result is because of the *
metacharacter/quantifier. It matches zero or more occurrences. I
assume that the first time it matches three characters. So it can
still match zero characters. So you get two replacement character
sequences.

On the other hand if you use + instead of * it matches three
characters only once. So the output for + is "new" rather than
"newnew".

At least that's my $0.02.

Andrew Watt MVP

On Fri, 30 Jun 2006 14:32:46 -0400, "Bob Weiner" <bob@engr.uconn.edu>
wrote:

>This is probably a regex question as opposed to powershell but I am confused
>by an example of the -replace operator on page 89 in the user guide:
>
>> "abc" -replace "\w*", "new"

>newnew
>
>Why does this match twice? It seems to me that if \w* is greedy, it should
>take them all. If it is not, it should grab "", "a", "ab", "abc".
>
>The
>> "abc" -replace "\w?", "new"

>example is similar.
>
>bob

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
-ne operator PowerShell
-f operator PowerShell
64 bit binary or (-bor operator)? PowerShell
What does the $() operator do? PowerShell
Use of matching subexpressions in "-replace" operator 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