Solved

Combine regular expressions into one

Posted on 2007-11-30
5
2,629 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 84

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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
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…

749 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