newbie
newbie

Reputation: 1980

Regex for a url pattern

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

Upvotes: 1

Views: 102

Answers (2)

Christian Kuetbach
Christian Kuetbach

Reputation: 16060

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.

Upvotes: 0

Ωmega
Ωmega

Reputation: 43673

^(?: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

Upvotes: 5

Related Questions