Regex只发现1个问题



我需要找到line:

echo "PB702101 not executed"

:

if test ${csis_batch_completion} -le ${csis_batch_warning} ;then
echo "Running PB702101"
run ${csis_obj}/PB702101
display_completion
else
echo "PB702101 not executed"
fi

我正在使用:

^(?!#)..echo ""((?!""))(b.*?) not executed""$

但是我一直得到:

echo "Running PB702101"
run ${csis_obj}/PB702101
display_completion
else
echo "PB702101 not executed"

如何只得到最后一次出现的echo "XXXX未执行"?

你可以试试这个。它排除所有的预备文本,因此只得到最后一个。

@"(?s)echo[ ]""w+[ ]not[ ]executed""(?!.*echo[ ]""w+[ ]not[ ]executed"")"

https://regex101.com/r/TTTmwS/1

使用下面的正则表达式,您将与包含最新回显行后面的代码(在您的示例中为PB702101)的捕获组匹配整个字符串:

.+echos+"(.+?)not executed"

下面是用c#运行它的代码片段:

string input = 
"if test ${csis_batch_completion} -le ${csis_batch_warning} ;thenn" +
"  echo "Running PB702101"n" +
"  run ${csis_obj}/PB702101n" +
"  display_completionn" +
"elsen" +
"  echo "PB702101 not executed"n" +
"fi";

string pattern = @".+echos+""(.+?)not executed""";
Match match = Regex.Match(input, pattern);
if (match.Success)
Console.WriteLine("Capturing Group: " + match.Groups[1].Value);
else
Console.WriteLine("No match found.");

https://dotnetfiddle.net/SF5luh

最新更新