考虑以下通用正则表达式的 powershell 示例。
(?<=^|[\s)\n])[\n]*([^(\n\s]*)([(]([^)\n]*)[)])?
例子
$Matches = @()
$String = 'key1(value1) key2(value2)
key3(value3) key3.5
key4 key5(value5) GoofyStuff(I like kittens)
key6 key7 ForReal-Things(be sure to vote)
key8'
Write-Host start with
write-host $String
Write-Host
Write-Host found
([regex]'(?<=^|[\s)\n])([^(\n\s]*)([(]([^)\n]*)[)])?').matches($String) | foreach {
if ($_.Groups[1].Value) {
write-host "key at $($_.Groups[1].Index) = '$($_.Groups[1].Value)'"
if ($_.Groups[3].Value) {
write-host "value at $($_.Groups[3].Index) = '$($_.Groups[3].Value)'"
} # end if
} # end if
} # next match
产量
start with
key1(value1) key2(value2)
key3(value3) key3.5
key4 key5(value5) GoofyStuff(I like kittens)
key6 key7 ForReal-Things(be sure to vote)
key8
found
key at 0 = 'key1'
value at 5 = 'value1'
key at 13 = 'key2'
value at 18 = 'value2'
key at 27 = 'key3'
value at 32 = 'value3'
key at 40 = 'key3.5'
key at 48 = 'key4'
key at 53 = 'key5'
value at 58 = 'value5'
key at 67 = 'GoofyStuff'
value at 78 = 'I like kittens'
key at 95 = 'key6'
key at 100 = 'key7'
key at 105 = 'ForReal-Things'
value at 120 = 'be sure to vote'
key at 138 = 'key8'
概括
(?<=^|[\s)\n]*)
查找键的开头,假定每个键位于字符串的开头,或者紧跟在 \n、"(" 或空格 - 之后(?<=^|[\s)\n]*)
。这在 Java 中可能不起作用,因为在java 如何处理未定义大小的环视。(另见)
(?<=^|[\s)\n])
查找键的开头,假定每个键位于字符串的开头,或者紧跟在 \n、"(" 或空格 - 之后(?<=^|[\s)\n])
。这种环顾四周似乎在 C# 和 Powershell 中有效
([^(\n\s]*)
返回直到下一个 "("、\n 或 \s 之前的所有字符
([(]([^)\n]*)[)])?
如果存在,则返回参数内的值
循环内的额外逻辑测试 Matches 数组以验证是否找到了键名或值。在 powershell 中,$Matches 会自动填充字符串中的所有匹配项。