Q: Trying to build a regular expression to check pattern - 2 I was wondering if more checks could be added:  [Previously answered question](
Trying to build a regular expression to check pattern).  
The above problem is brilliantly solved using this regex by Brian Rogers:
/^([1-9]|[12][0-9]|3[01])(-([1-9]|[12][0-9]|3[01]))?(,([1-9]|[12][0-9]|3[01])(-([1-9]|[12][0-9]|3[01]))?)*$/  

[For Reference, posting the older problem again] 


*

*Start and end with a number

*Hyphen should start and end with a number  

*Comma should start and end with a number  

*Range of number should be from 1-31  

*If a number starts with a hyphen (-), it cannot end with any other character other than a comma AND follow all rules listed above.   


E.g.  2-2,1 OR 2,2-1 is valid while 1-1-1-1 is not valid.  
E.g.:
- 1-5,5,15-29
- 1,28,1-31,15
- 15,25,3 
- 1-24,5-6,2-9  
Could this go a step further and add other validations? 
1) The numbers should be in ascending order
E.g:
 - 1,2-3 - Valid
 - 4-6,23 - Valid
 - 23,4-5 - Invalid  
2) The numbers should not repeat
E.g:
a) 2,2,2 - Invalid
b) 2,3-6,3 - Invalid
c) 2,5,7-20 - Valid  
3) If possible
Number should not repeat if previously defined in range
E.g:
a) 2,3-6, 4 - Invalid, because 4 is already a number between 3 and 6
b) 12-16, 14-18 - Invalid, because 14,15 and 16 are already defined in 12-16
c) 9-13, 15, 17-19 - Valid  

A: Regular expression is supposed to check a pattern and not to handle business logic. The moment you start stating your problem with "if ... then ... else", it's not something regular expression is supposed to handle.

A: Regular expressions are very powerful and can potentially be used to solve the kind of challenge you have - even implementing some kind of business logic validation.
From an architectural and software engineering point of view, I would recommend you to restructure the problem and use rather program code to solve the issue. Let me explain why: The code will get 


*

*very difficult to read and understand

*error prone and not easy to test

*the cost of maintaining it will skyrocket


To summarize, even though regex are very powerful ( I am heavily using them as well in my applications) I would not overdo it. They are very elegant to solve some problems but get get very ugly to solve simple things.
