Solved

# Combine regular expressions into one

Posted on 2007-11-30
2,640 Views
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
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

LVL 27

Expert Comment

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

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

Author Comment

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

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

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

## Featured Post

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…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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 Month5 days, 11 hours left to enroll

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

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