Regex PowerShell:在不删除拆分模式的情况下拆分字符串?
我在这里尝试了解决方案表单,但我得到了错误(我的翻译)正则表达式。拆分未知???Regex PowerShell:在不删除拆分模式的情况下拆分字符串?,regex,powershell,split,Regex,Powershell,Split,我在这里尝试了解决方案表单,但我得到了错误(我的翻译)正则表达式。拆分未知??? 我需要将行拆分为一个字符串数组,但保留行的开头:“prg=PowerShell°” 我的线路 $l = "prg=PowerShell°V=2.0°dtd=20120602°user=kjuz°pwd=jhiuz°chk=876876°prg=PowerShell°V=2.0°dtd=20120602°user=kjuz°pwd=jhiuz°chk=876876°prg=PowerShell°V=2.0°d
我需要将行拆分为一个字符串数组,但保留行的开头:“prg=PowerShell°” 我的线路
$l = "prg=PowerShell°V=2.0°dtd=20120602°user=kjuz°pwd=jhiuz°chk=876876°prg=PowerShell°V=2.0°dtd=20120602°user=kjuz°pwd=jhiuz°chk=876876°prg=PowerShell°V=2.0°dtd=20120602°user=kjuz°pwd=jhiuz°chk=876876°"
[string]$x = Regex.Split($l, "(prg=PowerShell°)" )
$x
我得到:
+ [string]$x = Regex.Split <<<< ($l, "(prg=PowerShell°)" )
+ CategoryInfo : ObjectNotFound: (Regex.Split:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
+[string]$x=Regex.Split给你:
$regex = [regex] '(?=prg=PowerShell°)'
$splitarray = $regex.Split($subject);
要拆分,我们使用零宽度匹配(即,拆分时不会丢失字符)。为了做到这一点,我们向前看,看看接下来的字符是否是prg=PowerShell°
这就是正则表达式(?=prg=PowerShell°)
所做的。@gooly:$splitArray=$subject-split'(?=prg=PowerShell°)
就足够了。您不必在PowerShell中编写C。不过,作为一个补充说明,您将以这种方式(以及我们在这里看到的其他方式)在开始时得到一个空元素。为了避免这种情况,您可以通过在字符串开头显式不匹配来稍微修改正则表达式:(?)?。