BenM
BenM

Reputation: 53248

RegularExpression to match word in plural preceded by numbers

Consider the following string:

20 mins

I would like to create a RegularExpression in PHP that is capable of finding such a string using preg_match(). The Expression should be sufficiently dynamic so that it will recognize non-plurals and more options for the preceding numeric values. For example, the following examples should also be matched:

I have tried with the following, but it doesn't execute:

(^|\b)[0-99999*]\/b\min[s]?\b

Upvotes: 0

Views: 108

Answers (2)

Tafari
Tafari

Reputation: 3079

This pattern:

\b\d+(?:\.\d+)? mins?\b

It should match all cases you want including these:

•1 min
•0.5 mins
•999 mins
•0.51213123 mins
•2352352.51213123 mins

Upvotes: 0

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89629

you can use this pattern:

\b[0-9]+(?:\.[0-9]+)?\smins?\b

note: you can allow more than one space between the number and "mins" replacing \s by \s+

note2: a character class is a bag with characters, writing [0-99999*] has no sense. See the manual.

Upvotes: 3

Related Questions