Regular Expressions (Regex/RegExp)

Patterns of text that specifies a search pattern in text.

Online Tools


Commands

Grep Pattern Matching

grep -E '^0\d{9,10}$' example.txt
egrep '[a-zA-Z0-9]+\@\w\.com' example.txt


Example Patterns

Below is the basic examples.

[a-z]
# a, b, c, ..., y, z
[a-z]+
# abc, aaabbbcc, ghslkdja, ...
[a-zA-Z]
# a, A, b, B, ..., z, Z
[a-z]zz
# azz, bzz, czz, ...
[a-zA-Z]+zz
# azz, AaBbCdefzz, hkIkWEzz, ...
[a-z]?zz
# zz, azz, bzz, ...

[0-9]
# 0, 1, 2, ..., 9
[0-9]+
# 0, 00, 123, 77777, ...

[^a]pple
# bpple, cpple, zpple, 2pple, ...
[^a-c]pple
# dpple, epple, 2pple, ...

a.c
# aac, abc, a"c, a;c, a c, ...
a\.c
# a.c

abc?
# abc, ab

\d
# 0, 1, ..., 9
\D
# a, b, A, Z, ;, +, ?, ....
\w
# a, A, b, B, ..., z, Z, 0, 1, 3, ...9
\W
# !, ", #, ...
\s
# \r, \n
\S
# a, A, b, B, ..., 0, 1, ..., !, ", #, ...

a{3}
# aaa
abc{3}
# abccc
\d{3}
# 123, 444, 987, ...

a{2,5}
# aa, aaa, aaaa, aaaaa

a{3,}
# aaa, aaaa, aaaaa, aaaaaa, ...

a*
# <NONE>, a, aa, aaa, aaaa, ...
a+
# a, aa, aaa, aaaa, ...

^abc
# start with "abc"
xyz$
# end with "xyz"
EOF\$$
# end with "EOF$"

hello (john|jane)
# hello john, hello jane

(abc){3}
# abcabcabc

IPv4 Address

(\d{1,3}\.){3}\d{1,3}
# 127.0.0.1, 8.8.8.8, 255.255.255.255, ...

Email Address

(\w+)@(\w+)\.\w+(\.\w+)?
# user@examle.com, info@example.jp, ...

TEL

^0\d{9,10}$
# 0000000000, ...

^0\d{1,3}-\d{1,4}-\d{4}
# 000-000-0000, ...