Link to home
Start Free TrialLog in
Avatar of gromul
gromul

asked on

Combine regular expressions into one

I have two validating regular expressions I'm not sure how to combine into one:

(?=^\w{1,10}$)(?!^\d+$) #Alphanumeric string up to 10 chars, can't be all just numbers
(?=^\w{1,20}$)(?!^[a-zA-Z]+$) #Alphanumeric string up to 20 chars, can't be all just letters

The valid input string would be in format: A\B where A is the first regex and B is the second. Any tips on how to do this?

Thanks
Avatar of ddrudik
ddrudik
Flag of United States of America image

You cannot combine those with the rules you state, variable sizes 1-10, 1-20 etc. would not allow definition of which character positions belong to which pattern.

e.g.:
1ABC

"1" fails with your first rule and "ABC" fails with your second rule but "1ABC" passed with the first rule, your rules seem contradictory.  Please elaborate on the rules and provide specific examples.
(?=^\w{1,10}$)(?!^\d+$)|(?=^\w{1,20}$)(?!^[a-zA-Z]+$)
Avatar of gromul
gromul

ASKER

I'm not sure if it was clear that the valid input string must consist of two parts that are validated with the above expressions, and the parts are separated with a '\' character. Here's some valid input:

alpha0123\alpha0123   #two parts are valid and there's '\'

and invalid:

123\alpha0123     #first part is just numbers
alpha0123\alpha  #second part is just letters
alpha0123            #there's no '\'

Let's say I change the max length from 10 and 20 to 2 and 4. So the total max length is 2 + 1 + 4 = 7 (including the '\' character).

Then this is valid:

a1\ab12

and invalid:

a1b\ab12     # first part too long
a1\ab123     # second part too long
ASKER CERTIFIED SOLUTION
Avatar of ddrudik
ddrudik
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the question and the points.