Regex仅发现1个问题

pgky5nke  于 2023-01-14  发布在  其他
关注(0)|答案(2)|浏览(88)

我需要找到行:

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"

如何仅获得最后一次出现的带有"XXXX未执行"的回显?

eyh26e7m

eyh26e7m1#

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

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

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

b4lqfgs4

b4lqfgs42#

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

.+echo\s+"(.+?)not executed"

这是用C#运行它的代码片段:

string input = 
 "if test ${csis_batch_completion} -le ${csis_batch_warning} ;then\n" +
 "  echo \"Running PB702101\"\n" +
 "  run ${csis_obj}/PB702101\n" +
 "  display_completion\n" +
 "else\n" +
 "  echo \"PB702101 not executed\"\n" +
 "fi";
        
string pattern = @".+echo\s+""(.+?)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

相关问题