Home > Enterprise >  Regex to extract first number before or after a word
Regex to extract first number before or after a word

Time:08-27

I want to extract the first instance of a number from a string.

For example:

Certificate will expire in 23 day(s). Configured certificate remaining lifetime check is 30 day(s)

I am looking for will expire in number, in this case, 23.

Got this far, but couldn't figure out removing 'in' from the capture. in\s*(\d )

CodePudding user response:

You can use a positive lookbehind ?<= to ensure something is preceded by something else:

(?<=will expire in )\d

CodePudding user response:

[^0-9]*([0-9] ).* Explanation: Ignore anything not a number, then match a number, and then anything after.

  • Related