繁体   English   中英

正则表达式仅发现 1 次出现问题

[英]Regex find only 1 occurrence issue

我需要找到线:

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 未执行”的回显?

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

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

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

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

.+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://do.netfiddle.net/SF5luh

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM