View Single Post
Old 06-30-2006   #2 (permalink)
Andrew Watt [MVP]


 
 

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