[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Combine regular expressions into one

Posted on 2007-11-30
5
Medium Priority
?
2,659 Views
Last Modified: 2011-02-05
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
0
Comment
Question by:gromul
  • 3
5 Comments
 
LVL 27

Expert Comment

by:ddrudik
ID: 20384500
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.
0
 
LVL 85

Expert Comment

by:ozo
ID: 20384536
(?=^\w{1,10}$)(?!^\d+$)|(?=^\w{1,20}$)(?!^[a-zA-Z]+$)
0
 

Author Comment

by:gromul
ID: 20384921
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
0
 
LVL 27

Accepted Solution

by:
ddrudik earned 2000 total points
ID: 20385029
If I understand your requirements:
(?=^\w{1,2}\\\w{1,4}$)(?!^\d+\\.*$)(?!^.*\\[a-zA-Z]+$)
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 20385126
Thanks for the question and the points.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses
Course of the Month19 days, 14 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question