regex - 在字符上分割字符串,但加倍的字符是转义符

标签 regex powershell split

输入:

$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/

相关文章:

java - 使用正则表达式解析

powershell - 为什么用单个字符而不是单个字符串填充PSCustomObject?

javascript - 多个条目后字符串替换然后拆分

file - Hadoop 在 Java 中映射整个文件

Java 将文件行拆分为两个列表

c# - 如何使用 C# 解析 [STX] 和 [ETX] 之间的字符串 - 使用正则表达式或字符串函数拆分/附加输出

regex - 如何从oracle的正则表达式中排除一个单词?

c - Flex正则表达式以标识AWK正则表达式

powershell - Concat invoke-SqlCmd查询字符串不起作用

powershell - 用Powershell add-adgroupmember结束