Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 提取两个变量之间的多行_Regex_Powershell - Fatal编程技术网

Regex 提取两个变量之间的多行

Regex 提取两个变量之间的多行,regex,powershell,Regex,Powershell,我被困在一个代码中,我的要求是得到两个字符串变量之间的行,请建议一个正则表达式模式 输入: Device Name: test Status: Completed Results: [PUSH_TERMLET] Action completed successfully... Pushing configuration file 'PUSH_TERMLET' for IDX 60504 with mechanism SSH ****** Enable Mode Results ****** sh

我被困在一个代码中,我的要求是得到两个字符串变量之间的行,请建议一个正则表达式模式

输入:

Device Name: test
Status: Completed
Results:
[PUSH_TERMLET] Action completed successfully...
Pushing configuration file 'PUSH_TERMLET' for IDX 60504 with mechanism SSH
****** Enable Mode Results ******
sh run | sec ip access-list standard customer_internal
ip access-list standard customer_internal
 permit 204.79.49.116
 permit 159.12.130.0 0.0.0.31
 permit 10.108.30.0 0.0.0.255
 permit 10.108.141.0 0.0.0.127
 permit 10.108.172.0 0.0.0.255
 permit 10.108.160.0 0.0.0.31
test#
输出:

 permit 204.79.49.116
 permit 159.12.130.0 0.0.0.31
 permit 10.108.30.0 0.0.0.255
 permit 10.108.141.0 0.0.0.127
 permit 10.108.172.0 0.0.0.255
 permit 10.108.160.0 0.0.0.31
这是我正在尝试的代码

$content = Get-Content -Path "TestACL.txt"

$device_list = $content | Select-String -Pattern "device name" -AllMatches

foreach($temp in $device_list) {
    $temp.sp
    $pattern = "(?<=$temp|permit)(.*)"
    $content | Select-String -Pattern $pattern
}

除此之外,我还尝试了其他模式,它们都不适合我。

你的正则表达式有点接近。让我们探讨一下这个选项

$Content = (Get-Content "C:\temp\TestACL.txt") -join ' '
$variable = "test#"
$Output = ($Content  | Select-String -Pattern "(?:permit )(.*)(?= $($variable))").Matches.Value
?:permit将查找单词permit,包含空格,而?:将在匹配中包含它

?=test将包含单词test(包含空格),并且?=将匹配,但将其从主表达式后的结果中排除


.*将捕获第一个和最后一个条件之间的任何内容

您应该通过添加-split'?=permit'@Drew来撤消连接谢谢您的回答!它对我有用。是否可以在正则表达式模式中将test用作变量。请suggest@KomalB我已经更新了我的答案,能够使用变量而不是测试