Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace for entire line produces duplicate occurrence of replacement text

Simple question: why does

"x" -replace ".*", "y"

produce "yy" ?

like image 483
Elroy Flynn Avatar asked Nov 30 '11 06:11

Elroy Flynn


2 Answers

"x" -replace ".*", "y"

is the equivalent of

[Regex]::replace("x",".*","y")

The result yy that you see is based on how this works, as per MSDN:

Within a specified input string, replaces all strings that match a specified regular expression with a specified replacement string.

http://msdn.microsoft.com/en-us/library/e7f5w83z.aspx

The replace will find a string that matches the regular expression and replace it with the given replacement. Hence, the x replaced with y and then empty string is replaced with y and you get yy.

This can be verified by doing [Regex]::matches("x",".*") - it give two matches - one for empty string and one for x.

In terms of other regular expression engines, this happens because of the g or the global flag.

This can also be verified in Python as follows ( just to show that this is not limited to Powershell / .Net ):

>>> re.findall(".*","x")
['x', '']
like image 142
manojlds Avatar answered Oct 20 '22 08:10

manojlds


Because you match two times, * matches first the entire string since it's greedy. And then it matches nothing. Thanks to @Tim for his comment.

What you should use is .+.

like image 4
FailedDev Avatar answered Oct 20 '22 08:10

FailedDev