我在这里尝试了解决方案,但收到错误(我的翻译)Regex.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°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
出了什么问题?
最佳答案
给你:
$regex = [regex] '(?=prg=PowerShell°)'
$splitarray = $regex.Split($subject);
为了拆分,我们使用零宽度匹配(即,我们在不丢失字符的情况下拆分)。为此,我们向前查看下一个字符是否是 prg=PowerShell°
这就是正则表达式 (?=prg=PowerShell°)
的作用。
关于regex - PowerShell:拆分字符串而不删除拆分模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24007663/