1

I'm developing a dynamic controller which accept this urls:

  1. mock_test5 - mock_test1
  2. reviewer_test5
  3. reviewer_test4
  4. reviewer_test3.1
  5. reviewer_test3.2
  6. reviewer_test1

Note: reviewer_test2 is excluded

I'm having a hard time figuring it out. This is what I don't so far:

^(mock|reviewer)_test[1-5]$

I also don't know how do I interpret 3.1 and 3.2

9
  • Are this all possible URLs? Commented Nov 28, 2013 at 22:41
  • 2
    If you need to check it against exactly those 10 strings, why don't you just put them all in a List and use contains? Commented Nov 28, 2013 at 22:42
  • @ChristianKuetbach yes that the all possible urls Commented Nov 28, 2013 at 22:46
  • @DavidWallace I'm using Spring MVC and as I said it is a URL pattern Commented Nov 28, 2013 at 22:47
  • should all elements that are not in this list fail? (Is it ok to have false-positives?) Commented Nov 28, 2013 at 22:51

2 Answers 2

5
^(?:mock_test[1-5]|reviewer_test(?:[145]|3[.][12]))$

Regular expression visualization

^(?:mock_test(?:1|2|3|4|5)|reviewer_test(?:(?:1|4|5)|3\.(?:1|2)))$

Regular expression visualization

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

3 Comments

I know it is off topic but I just want to know what did you use to generate that chart?
Why all the ?: symbols? I don't see what difference they make here.
@DavidWallace - Non capturing grouping save resources
0

As you can see at this link: http://regexr.com?37dtn

^(mock_test[1-5])|reviewer_test([145]|3\.1|3\.2)$

^                  = start a line with
(mock_test[1-5])   = the string 'mock_test' followed by a number from 1 to 5 
                     I think the '(' and ')' are not necessary
|                  = OR
reviewer_test      = the string 'reviewer_test'
([145]|3\.1|3\.2)  = the numbers 1,4,5 OR the number 3.1 OR the number 3.2
$                  = end of line

Update:

If it is a limited number I wouldn't use a regex, because it seems to be some kind of write once read never again code.

3 Comments

doesn't match mock_test2
If I don't use regex to this. I will have to write 10 duplicate controllers with the same logic inside.
One last thing before I accept your answer. Can you explain a bit the regex so I can create similar in the future?

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.