regex - PowerShell:拆分字符串而不删除拆分模式?

标签 regex powershell split

我在这里尝试了解决方案,但收到错误(我的翻译)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/

相关文章:

powershell - 从 Windows 任务计划程序执行 PowerShell

powershell - Set-StrictMode 不涵盖整个 PowerShell 脚本

postgresql - Postgresql 中的分割和顺序连接字符串部分

php - 分解逗号和管道上的字符串以创建两个数组

regex - 如何用一个正则表达式匹配命令行中的参数?

javascript - 如何从 FTP 地址获取基本 url?

sql-server - SQL Server 服务器中的正则表达式?

java - 我如何测试正则表达式的条件

powershell - 任何 AWS Powershell 命令失败并显示消息 "Invalid URI: The hostname could not be parsed."

python - 从具有多个字符串的列制作 get_dummies 类型数据框的最快方法