Solved

Combine regular expressions into one

Posted on 2007-11-30
5
2,610 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 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SSIS Expression Editor 1 35
c# find folder path with regex 2 82
VBA Test For Valid Number Format 6 44
Powershell regex followup 13 69
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…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now