Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
488 views
in Technique[技术] by (71.8m points)

Find numbers after specific text in a string with RegEx

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...