Regular Expression

Posted on by By Somen Sarkar, in Javascript | 0

Regular Expression in Java

Regular Expression or popularly known as Regex is a powerful features and help developers in many ways.
The following are some special characters with their behaviour

. Match any character except newline

\w Match any alphanumeric character

\s Match any whitespace character

\d Match any digit

\b Match the beginning or end of a word

^ Match the beginning of the string

$ Match the end of the string

Repetitions

* Repeat any number of times

+ Repeat one or more times

? Repeat zero or one time

{n} Repeat n times

{n,m} Repeat at least n, but no more than m times

{n,} Repeat at least n times

Negation

\W Match any character that is NOT alphanumeric

\S Match any character that is NOT whitespace

\D Match any character that is NOT a digit

\B Match a position that is NOT the beginning or end of a word

[^x] Match any character that is NOT x

[^aeiou] Match any character that is NOT one of the characters aeiou

Construct Matches

[abc] Matches a, or b or c. This is called a simple class, and it matches any of the characters in the class.
[^abc] Matches any character except a, b, and c. This is a negation.
[a-zA-Z] Matches any character from a to z, or A to Z, including a, A, z and Z. This called a range.
[a-d[m-p]] Matches any character from a to d, or from m to p. This is called a union.
[a-z&&[def]] Matches d, e, or f. This is called an intersection (here between the range a-z and the characters def).
[a-z&&[^bc]] Matches all characters from a to z except b and c. This is called a subtraction.
[a-z&&[^m-p]] Matches all characters from a to z except the characters from m to p. This is also called a subtraction.

Examples

1. Username Regular Expression Pattern
^[a-z0-9_-]{3,15}$

2. Password Regular Expression Pattern
((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})
(
(?=.*\d) # should have one digit from 0-9
(?=.*[a-z]) # should have one lowercase characters
(?=.*[A-Z]) # should have one uppercase characters
(?=.*[@#$%]) # should have one special symbols in the list "@#$%"
. # match anything with previous condition checking
{6,20} # length at least 6 characters and maximum of 20
)



3. Email Regular Expression Pattern
^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+

4 Image File Extension Regular Expression Pattern
([^\s]+(\.(?i)(jpg|png|gif|bmp))$)

5.Time in 24-Hour Format Regular Expression Pattern
([01]?[0-9]|2[0-3]):[0-5][0-9]

6. Date Format (dd/mm/yyyy) Regular Expression Pattern
(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)

Reference- internet

logo

Best Open Source Business Intelligence Software Helical Insight is Here

logo

A Business Intelligence Framework

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments