regular expression for special characters

Tag: regex , validation Author: twinkle12 Date: 2010-10-19

I am using this regular expression for validating email address for example:


and now I want to write a validation expression for not using one special character, like ";" in my example...

P.S. I know how to do this in JavaScript with the keyCode function for example, but I need it as a simple regular expression...

What do you mean, a validation expression for not using like ";" in your example? The English doesn't have a clear parse. Do you mean you want a regex that forbids semicolon anywhere in a string, that you want to add that restriction to your long emailish thing, or something else?
Your regex is already not allowing semicolons anywhere in the string. Can you show some examples of what should and shouldn't be matched?

Best Answer

I'm guessing that you're looking for an entirely new regex that has nothing to do with your email regex, and that the only rule for this regex is: "Match any string that does not contain a semicolon."

This is easy. Use the following regex:



^ matches the start of the string.

[^;]* is a negated character class. As the first character inside brackets, the ^ takes on a new meaning as "anything but". So this expression means "Match a character unless it's a semicolon". The * allows any number of repetitions, including zero.

$ matches the end of the string.


Dear Mr. Pietzcker ... thank you very much for your good and clear explanation...