- What is regular Expression?
Regular expression is a sequence of characters called as pattern which is used in search operations.
- Characters used in Regular Expression.
- .Matches any character
- ?Matches zero occurrence or only one time.
Ex: ab? . It matches (a – Zero occurrence of ‘b’) ( ab – One occurrence of ‘b’) - *Matches zero or many times.
Ex: ab*. It matches a, ab, abb, abb - +Matches at least one or many times.
Ex: ab+. It matches ab, abb, abbb - X{n,}Matches the character(s) at least n times.
Ex: a{2}. It matches aa, aaa, aaa - X{n,m}Matches the character(s) at least n times and not more that m times.
- [ ]Matches single character with in the bracket.
Ex: [ab] matches a or b or c - [^ ]Opposite to [ ]. It matches a character which is not in the bracket.
Ex: [^ab] matches any character other than a and b. - ^Beginning of the line
- $End of the line
- Character classes used in Regular Expression.
- [a-z]Matches lowercase alphabets a to z
- [A-Z]Matches Uppercase alphabets A to Z
- [a-zA-Z]Matches alphabets
- [a-zA-Z0-9]Matches Alphanumeric
- \wAlphanumeric with underscore character [a-zA-Z0-9_]
- \WNegation of \w. Non word characters. [^\w]
- [0-9]Digits 0 to 9
- \dDigits
- \DNon Digits. [^0-9]
- \sWhite Space
Validate Alphabets
Example: Simple program to validate alphabets using regular expression.
Validate Digits
Example: Simple program to validate digits using regular expression.
Validate Alpha Numeric
Example: Simple program to validate alpha numeric using regular expression.
Validate Phone no
Example: Simple program to validate phone no using regular expression.
Validate Email Id
Example: Simple program to validate email id using regular expression.