Collection of different Regex Examples


Regex is a very important aspect in the programming language. Its full form is Regular Expression Also called as REGEX in Short form. Our works and code becomes very clean and short and accurate if we apply regex and solve the problem. Applicable with JavaScript, VB, .Net etc. etc.

Sample Regular Expressions used for consumer websites are below. Sample error messages are included on some.

Address>


Regex: ^[\w#][a-zA-Z0-9/#',-\.\u0020\u00A1\u00AA\u00AB\u00BA\u00BB\u00BF-\u00FF\u0160\u0161\u0178\u017D\u017E\u0192\u2039\u203A]+$
Error Message: The value is not a valid address.

Email


Regex: ^([a-zA-Z0-9_\-\.]+)@(([[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$
Error Message: The email address does not adhere to the proper format.

Last Name


Regex: ^[a-zA-Z'\-\u0020\u00A1\u00AA\u00AB\u00BA\u00BB\u00BF-\u00FF\u0160\u0161\u0178\u017D\u017E\u0192\u2039\u203A]+$
Error Message: The value is not a valid last name.

Password


Regex: (?!^[0-9]*$)(?!^[a-zA-Z]*$)^([\S.]{6,20})$
Error Message: Password must contain at least one number, one letter, and be between 6-20 characters in length.

Phone Number


Regex: ^(1?(?: |\-|\.)?(?:\(\d{3}\)|\d{3})(?: |\-|\.)?\d{3}(?: |\-|\.)?\d{4})$
Error Message: The phone number has been entered in an unrecognized format.

Salutation


Regex: ^(Miss\.)|(Miss)|(Ms\.)|(Ms)|(Mrs\.)|(Mrs)|(Mr\.)|(Mr)$
Error Message: Salutation must be Miss, Ms., Mrs., or Mr.

Screen Name


Regex: (?!^[0-9]*$)^([\w_\-]{3,25})$
Error Message: The screen name must start with a letter, be between 3 and 25 characters in length, and contain only letters, numbers, hyphens and underscores.

Signature


Regex: ^[a-zA-Z'\-\u0020\u00A1\u00AA\u00AB\u00BA\u00BB\u00BF-\u00FF\u0160\u0161\u0178\u017D\u017E\u0192\u2039\u203A]+$
Error Message: The value is not a valid signature.

States & Provinces


Regex: ^(?:(A[ABEKLPRSZ]|BC|C[AOT]|D[CE]|F[LM]|G[AU]|HI|I[ADLN]|K[SY]|LA|M[ABDEHINOPST]|N[BCDEHJLMSTUVY]|O[HKNR]|P[AERW]|QC|RI|S[CDK]|T[NX]|UT|V[AIT]|W[AIVY]|YT))$
Error Message: State or province must be a valid two-letter code

Canada Only


Regex: ^(?:(AB|BC|MB|N[BLSTU]|ON|PE|QC|SK|YT))$
Error Message: Invalid province code

Zip Code


Regex: ^(\d{5})(-\d{4})?$
Error Message: A valid US ZIP code contains a series of digits in the pattern "00000" or "00000-0000".

Canada Only


Regex: ^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$
Error Message: Invalid Canadian postal code


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: