Regular Expression Examples

1.

.

Any character

2.

\.
dot character

3.

+

One or more character

4.

*

Zero or more characters

5.

H.+H
Greedy

6.

H.+?H

Non greedy

7.

^
not and also used as beginning of string.

8.

$

End of string

9.

Numbers in the format [12345-1234]
[\d]{5}-[\d]{4}

10.

Numbers in format[12345-1234(optional])
^[\d]{5}(?:-[\d]{4})?$

11.

Email
\w+@\w+\.\w{1,3}
^.+?@.+\.[a-z]{2,4}$

12.

Repeating words (is is)
([a-z]+)\s+\1

13.

Date (day/month/year-12/04/2014)
(0[1-9]|1[0-2])\/(0[1-9]|[12][0-9]|3[0-1])\/([0-9]{4})

14.
Time (12:00 PM)

(1[0-2]|0?[1-9]):([0-5][0-9])\s([PA]M)

15.

IP
192.168.1.1
([0-9]{1,3}\.){3}[0-9]{1,3}

16.
URL

https://www.googe.com
http://www.googe.com
file://www.googe.com

(https?|ftp|file)://.+$

17.
file with image extension

^.+(jpg|gif|png)$

(^.+(jpg|gif|png)$)

18.
href=([“‘])([^”‘]+)([“‘])

<a href=”www.google.com” title=”this” >

19.

display:box;
box-shadow:1px 2px 3px black;
box-sizing:100% 100%;

^box-(shadow|sizing)[^;]+
^box-(shadow|sizing).+$

20.

Negative look behind
display:box;box-shadow:1px 2px 3px black;box-sizing:100% 100%;
(?<!:)box[^;]+

21.

postive look ahead

jawad@gmail.com
ali@yahoo.com

@.+(?=\.[a-z]{2,4})

22.

Negative look behind

jawad@gmail.com
ali@gmail.com
jawad@hotmail.com

(?<!jawad)@.+

23.

Positive look behinde
(?<=jawad)@.+

24.

Positive look behind
box-sizing
shape-sizing
(?<=box-)sizing

25.

negative look behind
(?<!box-)sizing

26.

positive look ahead

box-sizing
box-shadow

box(?=-shadow)

27.

negative look head

box(?!-shadow)

Advertisements