0

I want to be able to match all the following cases below using Ruby 1.8.7.

/pages/multiedit/16801,16809,16817,16825,16833
/pages/multiedit/16801,16809,16817
/pages/multiedit/16801
/pages/multiedit/1,3,5,7,8,9,10,46

I currently have:

\/pages\/multiedit\/\d*

This matches upto the first set of numbers. So for example:

"/pages/multiedit/16801,16809,16817,16825,16833"[/\/pages\/multiedit\/\d*/]
# => "/pages/multiedit/16801"

See http://rubular.com/r/ruFPx5yIAF for example.

Thanks for the help, regex gods.

2 Answers 2

2
\/pages\/multiedit\/\d+(?:,\d+)*

Example: http://rubular.com/r/0nhpgki6Gy

Edit: Updated to not capture anything... Although the performance hit would be negligible. (Thanks Tin Man)

The currently accepted answer of

\/pages\/multiedit\/[\d,]+

may not be a good idea because that will also match the following strings

.../pages/multiedit/,,,
.../pages/multiedit/,1,

My answer requires there be at least one digit before the first comma, and at least one digit between commas, and it must end with a digit.

Sign up to request clarification or add additional context in comments.

1 Comment

Be careful using (...) in patterns because they're capturing. Instead you should use (?:...) which is non-capturing.
1

I'd use:

/\/pages\/multiedit\/[\d,]+/

Here's a demonstration of the pattern at http://rubular.com/r/h7VLZS1W1q

[\d,]+ means "find one or more numbers or commas"

The reason \d* doesn't work is it means "find zero or more numbers". As soon as the pattern search runs into a comma it stops. You have to tell the engine that it's OK to find numbers and commas.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.