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
Jay LeporeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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


^(?=.{6})(?!.{13})(?!.*[)(*,\\'~^=/])(?!.* .* )
0
gman84Commented:
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]*$
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

mark_harris231Commented:
The negative lookahead addresses the space count.  The expression allows ANY characters except those explicitly excluded.

^((?!(.*? .*? ))([^\)\(\*,\\'~\^=/]{6,12}))$
0
mark_harris231Commented:
ozo - it doesn't appear that your expression actually contained a capture group.  It (mostly) works if you add (.*) to the end, but allows 13 characters instead of the 12 character max specified.  It's also a little chunkier than it needs to be.

Through your example, however, I did learn that it's not necessary to escape the metacharacters used by regex (e.g., * and ^) when included within the square brackets (except for the backslash\).  Thanks for that.

With that in mind, my solution further reduces to:

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


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ozoCommented:
A capture group was not asked for, the question only asked for verification, and my expression does not allow 13 characters
0
Jay LeporeAuthor Commented:
Complete, accurate and easy to follow example.  Worked immediately upon insertion.

Thanks.

Jay
0
hieloCommented:
>>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

0
hieloCommented:
@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.
0
ozoCommented:
> 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
0
mark_harris231Commented:
>>"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.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.