# Regular Expression for validation!

• 08-08-2013, 12:38 AM
harishrajasekaran
Regular Expression for validation!
Hello everyone,

I'm trying to form a regular expression for a form validation where I would accept Alphanumeric characters and a maximum of 3 special characters which includes only (. ' ` -)

I came up with this -
[A-Za-z0-9]*[-. '`]{0,3}

But this won't allow me to add special characters at the beginning. Example it would fail for this string : "-sampletext-"

So if I go this way -
[A-Za-z0-9]*[-. '`]{0,3}[A-Za-z0-9]*

I would solve the above case, but still it would fail as in for the string : "-sample-sample-"

Could someone help me with this where I can enter the alphanumeric and special characters (max of 3) at random places. I gave a try with OR operators - not very lucky. May be I'm wrong.

Thanks a lot for your help guys.
• 08-08-2013, 05:22 AM
jim829
Re: Regular Expression for validation!
Well, I used these two patterns.
Code:

```String sp = "([-`'.]{0,3})"; // subpattern String regex = sp + "[\\w&&[^_]]*" + sp + "[\\w&&[^_]]*" + sp;```
Then I just did regular matches on the regex. When a match was found, I just summed the string lengths of the capture group to check that it was less than 4. Note that in my version, three special characters will match with no alphanumerics present. This worked in all cases I checked (but it only takes one for failure).

I suspect, it would be more straight forward to just write a method checking for alphanumerics in combination with a fall thru switch statement for your special characters. As soon as the special characters exceed 3, then the validation fails.

Regards,
Jim
• 08-08-2013, 07:26 AM
DarrylBurke
Re: Regular Expression for validation!
Quote:

Originally Posted by harishrajasekaran
I'm trying to form a regular expression for a form validation where I would accept Alphanumeric characters and a maximum of 3 special characters which includes only (. ' ` -)

-- do you allow "(.)" ?

Maybe something like
Code:

`replaceAll(input, "[^(. ' ` -)]").length() < 3  && input.matches("[a-zA-Z0-9(. ' ` -)]*");`
Check whether any of your special characters need escaping inside a character class.
• 08-08-2013, 05:26 PM
harishrajasekaran
Re: Regular Expression for validation!
Thank you for your response Jim.
I tried your approach too. Where even if I give the string -

"---sample---sample---"

It would allow it even with 9 special characters. But the first condition is to stop the customer's from entering more than 3 special characters. Am I making sense?
• 08-08-2013, 05:31 PM
harishrajasekaran
Re: Regular Expression for validation!
Would love to finish this off with Java code. But we've a common validation module that accepts only regular expressions for each field. So once you form a regular expression and associate it with that field, it would take care of validating that. By field I mean firstname or address line 1 or address line 2 etc. But keeping your approach in mine, can I add an OR (||) conditions to my regular expression? Let me know what you think about it.

Thank you for your response dude.
• 08-08-2013, 06:27 PM
jim829
Re: Regular Expression for validation!
But you also have to sum the String lengths of the capture groups which would have invalidated the match.

Regards,
Jim
• 08-08-2013, 08:32 PM
JosAH
Re: Regular Expression for validation!
Would the following be of any help? Let rc be the regular expression of one regular character, let sc be the same for the special characters; you want this rc*sc?rc*sc?rc*sc?rc*

kind regards,

Jos