dodecafonico
dodecafonico

Reputation: 305

Regex to match a number with something or nothing

I must match a string that can come up in 3 ways(numbers are variable and only for example illustration).

1-100 of 
1-25 of 
<nothing>

So I need a Regex that matches strings like "1-100 of ", "1-25 of " or nothing.

How could I accomplish this with a regex? Thank you very much!

Upvotes: 2

Views: 458

Answers (1)

hjpotter92
hjpotter92

Reputation: 80639

The simplest approach would be anchoring the expression to the end of string:

(\d+) results$

For the updated question: you'd still use an anchor, but now, you'll use it on both sides:

^((?:.+) )?\d+ results$

The result will get stored in the $1 matched group.

Upvotes: 5

Related Questions