Link to home
Start Free TrialLog in
Avatar of Jay Lepore
Jay Lepore

asked on

Regular Expression Needed

I am looking for our experts to provide an regex which will be used in php though I think this doesn't matter to you.

I need to verify a field
- has a minimum of 6 characters
- has a maximum of 12 characters
- can contain alpha, numeric and special characters
- does not contain any of these characters ) ( * , \ ' ~ ^ = /
- can contain only one white space

Should test properly here: http://www.titeurl.com/regextest

Thank you in advance.

Jay
CompuMatter
Avatar of Aaron Tomosky
Aaron Tomosky
Flag of United States of America image

Not an answer:
Check out expresso http://www.ultrapico.com/Expresso.htm
Makes life easy
- can contain alpha, numeric and special characters
Does this mean cannot contain non alpha, numeric and special characters?
What are "special characters"?


^(?=.{6})(?!.{13})(?!.*[)(*,\\'~^=/])(?!.* .* )
Not entirely sure how your going to do the "only one white space" aspect in a single expression, you might be better off using two expressions:

(1) Satisfy the other criteria: ^[^)(*,\'~=/^]{6,13}$
(2) Ensure that no more than one blank space appears ^[^\s]*(\s)?[^\s]*$
The negative lookahead addresses the space count.  The expression allows ANY characters except those explicitly excluded.

^((?!(.*? .*? ))([^\)\(\*,\\'~\^=/]{6,12}))$
ASKER CERTIFIED SOLUTION
Avatar of mark_harris231
mark_harris231
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
A capture group was not asked for, the question only asked for verification, and my expression does not allow 13 characters
Avatar of Jay Lepore
Jay Lepore

ASKER

Complete, accurate and easy to follow example.  Worked immediately upon insertion.

Thanks.

Jay
>>ozo - it doesn't appear that your expression actually contained a capture group.
Why would that be necessary.? If the regex is anchored to the beginning (^), and if the regex makes sure the input does not exceed max chars, then the "capturing" that you are doing should not be necessary because the entire input should be equal to the "capturing" you are (unnecessarily) doing.

>>but allows 13 characters instead of the 12 character max specified.
Out of mere curiosity, what input string did you try to backup this claim?

>> not necessary to escape the metacharacters used by regex (e.g., * and ^) when included within the square brackets (except for the backslash\)
In javascript (which is what the poster wants), you will also need to escape the forward slash.

>>With that in mind, my solution further reduces to
as implied above, you can reduce further by getting rid of the unnecessary capturing (and you have to escape the forward slash)
<script type="text/javascript">

var re=/^(?!(?:.*? .*? ))[^\)\(*,\\'~^=\/]{6,12}$/;

var tests = ["hi","wrong andlength",'123456789012','1234567890123','one space','co  rrect?',"correct","co rre ct",'\\invalid','invalid/','#@$yes&%','incorrect~','(no wrong','wrong)',' wrong',' wrong ','1234*nope','12 34,nope',"nope'",'~nope ','^wrong12','1234yes-_','nope=wrong', 'Françoise', 'ENCYCLOPÆDIA', 'ÐÂRTH ÝÃÐÉR'];
for(var i=0,limit=tests.length;i<limit;++i)
{
	document.write("Original:\t&#187;"+tests[i]+"&#171;\n");
	document.write("Valid:\t\t" + (re.test(tests[i])?"Yes":"No") +"\n\n");
}
</script>

Open in new window

@ozo:
Sorry to see you get robbed like that.  Clearly that 13 character comment:
 "...allows 13 characters instead of the 12 character max specified..."
was completely wrong AND too bad the poster does not understand the efficiency behind your regex.  For what's worth, you get an A+ in my book.
> In javascript (which is what the poster wants), you will also need to escape the forward slash.
The question said:
> Should test properly here: http://www.titeurl.com/regextest
which did not require escaping the forward slash.

My regexp could allow 13 characters if one of them is a newline,
but I don't see how to enter a newline in http://www.titeurl.com/regextest
>>"Out of mere curiosity, what input string did you try to backup this claim?"

I tested using the string "1234567890123".  This was after adding the (.*) as I indicated, which may be the reason for the 13th character.

>>"In javascript (which is what the poster wants), you will also need to escape the forward slash."

I saw no request for javascript?  He referenced PHP in the original post.

>>"as implied above, you can reduce further by getting rid of the unnecessary capturing (and you have to escape the forward slash)"

In response to that, I would simply point to the OP closing comment: "Complete, accurate and easy to follow example.  Worked immediately upon insertion."


Speaking more generally - lighten up.  I'm not in the practice of trying to "rob" anyone and I'm more than willing to acknowledge and accept corrections/clarifications/alternatives without getting bent out of sorts.  Public divisiveness within these forums is counter-productive.  There are mechanisms to object to the accepted solution if felt warranted.