对于更换整条生产线生产的替换文本的重复发生 [英] Replace for entire line produces duplicate occurrence of replacement text
问题描述
简单的问题:为什么
X-replace*,Y
农产品YY?
X-replace*,Y
是等效
[正则表达式] ::代替(X,*,Y)
结果 YY
,你看到的是基于它是如何工作,根据MSDN:
在一个指定的输入字符串替换所有字符串那场比赛一 指定正EX pression与指定的替换字符串。
<一个href="http://msdn.microsoft.com/en-us/library/e7f5w83z.aspx">http://msdn.microsoft.com/en-us/library/e7f5w83z.aspx
的取代会找到一个匹配正则前pression串,并将它与给定替换替换。因此,与y,然后空字符串替换x被替换为y和你 YY
。
这可以通过执行 [正则表达式] ::匹配验证
(X,*) - 它给出了两个比赛 - 一个是空的字符串和一个对于x
在其他规则前pression引擎而言,出现这种情况是因为在先按g
或全局标志。
这也可以在Python的验证内容如下(只是为了显示这不限于Powershell的/ .NET):
&GT;&GT;&GT;通过re.findall(。*,×)
['X','']
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', '']
这篇关于对于更换整条生产线生产的替换文本的重复发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!