Find digits (below certian number) on the page from specific work until specific last word is found

Viewed 32

Would like some support here with a Regex.

Regular expression:

(\scaptain\s.*b([1-9]|[12][0-9]|3[01])\s[0-4][0-9][0-9]\s.*morgan\s)

Test string:

bla bla bla captain bla bla 1 250 2 450 9 501 11 900 21 900 30 400 31 1023 morgan bla bla bla

Question: Match a 3 digit number below 500 after the word "captain"and until the word "morgan" appears. Goal: Match the 3 digit number below 500 All support much appreciated!

1 Answers

You could do that with positive and negative lookahead:

\b[0-4][0-9][0-9]\b(?=.*morgan)(?!.*captain)

  • \b word break
  • [0-4][0-9][0-9] and number from 000 to 499
  • (?=.*morgan)has to be followed by morgan somewhere
  • (?!.*captain) can not be followed by captain anywhere

if you don't want to have 000 to 099 included then it would be \b[1-4][0-9][0-9]\b(?=.*morgan)(?!.*captain)

What is not covered is, that the string has to contain captain.

example: https://regex101.com/r/uFtBiy/1

Related