Monday, April 1, 2013

Common RegEx Expression



Allow a-zA-Z0-9 ( no space)
^[a-zA-Z0-9]+$
Allow a-zA-Z0-9 and space ( look at \s)
^[a-zA-Z0-9\s]+$ 
Allow a-zA-Z0-9 , SPACE and _
^[\w\s]+$
Only numeric 0-9
^[\d]+$  OR  ^[0-9]+$ OR [\d]+
Only numeric + dot ( amounts)
^[\d.]+$ OR ^[0-9.]+$ OR  [\d.]+ 
String of Length exactly 8 
^[\w]{8}$ 
A string of min length 1 and max length 8
^[\w]{1,8}$
A number of fixed format 3.2 (e.g. 111.22)
^[\d]{3}\.[\d]{2}$
A number of min 1.0 and max 111.22 (e.g. 1 , 1.2, 22,22,444.22, but not 4444.44, 55.555)
^[\d]{1,3}\.[\d]{0,2}$



NOTES :
[1]
^ signifies start of string
$ signifies end of string
[] signifies one character
+ multiple characters

e.g. : ^[a-z]+$ : from start to end, for multiple characters ( no length restriction), verify that each character is in a-z.

[2]
\d : numeric
\s : white spaces
\w : numeric,character ( a-zA-Z) , underscore

\d\s : number + space
\w\s : alphanum + white space + _

[3]
"." matches any single character, but when included in a class [], it looses it special meaning and becomes charater dot "."

[4]

Note that . has to be escaped "\."
^[\d]{3}\.[\d]{2}$
In the above string, "." will be compulsory.
To make the "." optional, follow it with {0,1} :
^[\d]{1,3}\.{0,1}[\d]{0,2}$

No comments:

Post a Comment

How to check local and global angular versions

 Use the command ng version (or ng v ) to find the version of Angular CLI in the current folder. Run it outside of the Angular project, to f...