Eugene van der Merwe
Eugene van der Merwe

Reputation: 4670

Regular expression to match multiple numbers in a sentence where said numbers are sometimes omitted

Given the sentences below, how do I match all numbers specified by "Run time", and optionally match just the "Run time" numbers instead of what's on the rest of the page (see example 3):

Example 1:

Run Time: 72 hours, 14 minutes, and 20 seconds

Example 2 (only two numbers, singular seconds):

Run Time: 28 minutes, and 1 second

Example 3 (only seconds but numbers further down not interested in them):

Run Time:

24 seconds

This notice is the result of a request from “cPanel Backup System”.

The system generated this notice on Thursday, July 28, 2022

The closest match so far but doesn't work:

/Run Time: (\d+) hour.+(\d+) minute.+(\d+) second/

That matches the 72, but only the 4 and 0 of 14 and 20.

I'm using this tool to test: https://regex101.com/

Upvotes: 0

Views: 52

Answers (1)

Ivan Wu
Ivan Wu

Reputation: 191

Exclude the number and capture them

[^0-9]+(\d+)[^0-9]+(\d+)[^0-9]+(\d+)

Upvotes: 2

Related Questions