输入:
$string = "this-is-just-an--example"
输出:
this
is
just
an-example
围绕Regex.Split和“-[^-]”或“-([^])”进行了各种尝试。
无效的示例:
[regex]::Split( $string, "-[^-]" )
[regex]::Split( $string, "-([^-])" )
当然,我可以使用String.Split并进行迭代,并意识到空字符串意味着我遇到了转义字符……但这是丑陋的代码。
附言尝试搜索被骗了几分钟,没有找到任何东西。
最佳答案
使用先行断言和后向断言,然后进行替换以消除剩余的 double 字符:
$string = "this-is-just-an--example"
$string -split '(?<!-)-(?!-)' -replace '--','-'
this
is
just
an-example
关于regex - 在字符上分割字符串,但加倍的字符是转义符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9917873/