使用 RegEx 在字符串中的特定文本后查找数字

Find numbers after specific text in a string with RegEx

我有一个如下所示的多行字符串:

2012-15-08 07:04 Bla bla bla blup
2012-15-08 07:05 *** Error importing row no. 5: The import of this line failed because bla bla
2012-15-08 07:05 Another text that I don't want to search...
2012-15-08 07:06 Another text that I don't want to search...
2012-15-08 07:06 *** Error importing row no. 5: The import of this line failed because bla bla
2012-15-08 07:07 Import has finished bla bla

我想要的是借助 RegularExpression(使用 PowerShell)提取所有有错误的行号。所以我需要找到“*** Error importing row no.”和后面的“:”之间的数字,因为这总是会给我行号。

我查看了其他各种 RegEx 问题,但老实说,答案对我来说就像中文。

尝试在 http://regexr.com/ 的帮助下构建正则表达式,但到目前为止还没有成功,例如使用以下模式:

"Error importing row no. "(.?)":"

有什么提示吗?

试试这个表达式:

"Error importing row no\. (\d+):"

DEMO

这里需要理解量词和转义序列:

  • .任意字符;因为你只想要数字,所以使用 \d;如果您指的是句点字符,则必须使用反斜杠 (\.)
  • 将其转义
  • ? 零个或一个字符;这不是您想要的,因为您可以在第 10 行出现错误,并且只接受“1”
  • +一个或多个;这对我们来说足够了
  • *任意字符数;将此与 .* 一起使用时必须小心,因为它会消耗您的全部输入

非常简单。现在您的引用将导致您编写的正则表达式出错。试试这个:

$LogText = ""#Your logging stuff
[regex]$Regex = "Error importing row no\. ([0-9]*):"
$Matches = $Regex.Matches($LogText)
$Matches | ForEach-Object {
    $RowNum = $_.Groups[1].Value #(Waves hand) These are the rows you are looking for
}

可能有多种方法,下面显示的一些简单方法可能会有所帮助:-

我把你的日志记录在一个名为 temp.txt 的文件中。

cat temp.txt | grep " Error importing row no." | awk -F":" '{print }' | awk -F"." '{print }'

OR

cat temp.txt | grep " Error importing row no." | sed  's/\(.*\)no.\(.*\):\(.*\)//'