Find numbers after specific text in a string with RegEx

不羁岁月 提交于 2019-12-30 06:04:47

问题


I have a multiline string like the following:

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

What I want is to extract all row numbers that have errors with the help of RegularExpression (with PowerShell). So I need to find the number between "*** Error importing row no. " and the following ":" as this will always give me the row number.

I looked at various other RegEx question but to be honest the answers are like chinese to me.

Tried to built RegEx with help of http://regexr.com/ but haven't been successful so far, for example with the following pattern:

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

Any hints?


回答1:


Try this expression:

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

DEMO

Here you need to understand the quantifiers and escaped sequences:

  • . any character; as you want only numbers, use \d; if you meant the period character you must escape it with a backslash (\.)
  • ? Zero or one character; this isn't what do you want, as you can here an error on line 10 and would take only the "1"
  • + One or many; this will suffice for us
  • * Any character count; you must take care when using this with .* as it can consume your entire input



回答2:


Pretty straight forward. Right now your quoting is going to cause an error in the regex you wrote up. Try this instead:

$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
}



回答3:


THere could be multiple ways , few simple ones shown below might help:-

I took your log in a file called temp.txt.

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

OR

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


来源:https://stackoverflow.com/questions/30639136/find-numbers-after-specific-text-in-a-string-with-regex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!