-1

I'm trying to find a regex which searches for every numbers if a keyword exists in the paragraph.

For example, if my keyword is something, with this paragraph:

20
30
abc
40
def

something

my regex should get 20, 30 and 40. But for this one:

50
60
xyz

it should get nothing.

Can you guys help me out to find a good regex. Thank you so much! I'm using PCRE

1 Answers1

2

You can use this regex in single line (DOTALL) mode with a lookahead assertion:

(?s)\b\d+(?=.*\bsomething\b)

It will match numbers only when there is a word something ahead in input.

RegEx Demo

RegEx Details:

  • (?s): Enable single line mode so that dot also matches newlines
  • \b: Match a word boundary
  • \d+: Match 1+ digits
  • (?=.*\bsomething\b): Positive lookahead to assert that we have a word something ahead of us from current position
anubhava
  • 761,203
  • 64
  • 569
  • 643