Simple question: why does
"x" -replace ".*", "y"
produce "yy" ?
"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', '']
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 .+
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With