Skip to main content

Regex

regex - From Pattern to Pattern

Patternregex
from a to z[a-z]
from A to Z[A-Z]
from a to z and A to z[a-zA-Z]
from 0 to 9[0-9]
a or b or c[abc]
Except d or e[^de]

regex - How many times

Occursregex
0 or 1 times[ ]?
0 or more times[ ]*
1 or more time[ ]+
n times[ ]{n}
n or more times[ ]{n,}
Minimum p times and Maximum q times[ ]{p,q}

regex - Meta characters

Meta CharacterregexExplain
\d[0-9]Digit 1 to 9 - Only One digit matching
\D[^0-9]Not a digit
\w[a-zA-Z_0-9]Word containing characters a to z, A to Z, 0 to 9 and _
\W[^\w]Not a word character
\s[\s]Whitespaces - spaces, tab, newline
\S[\S]Not Whitespaces
\b[\b]Word boundary
\B[\B]Not a word boundary
^[^]Beginning of a string
$Hello[$]Ending of the string
|[a|b]Either or - a or b
()(,|-)Capture GROUP - these are grouped and retrieved with in the expression

regex - email

[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+

regex - sample

Example to test the working of regex:

emails.txt
abcd@test.com
lkjsdlf lskjdfl kjasdf hello@gmail.co.in
av12_name.com
hello_kjfd@cccn.com
lkjsdf12@12a.com
acd.kjsd.kk@gg.com
grep emails from emails.txt
grep -oP '[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+' emails.txt
output
abcd@test.com
hello@gmail.co.in
hello_kjfd@cccn.com
lkjsdf12@12a.com
acd.kjsd.kk@gg.com

regex - phone

regex - phone - no grouping - basic

phone.txt
123 456 7899
123.456.7899
(123).456.7899
123-456-7899
No grouping and basic - also observe \s, try placement of \s in [\s.-]
grep -oP '[0-9]{3}[\s.-][0-9]{3}[.|\-|\s][0-9]{4}' phones.txt
output of phone
123 456 7899
123.456.7899
123-456-7899

regex - phone - no grouping - simplify

No grouping and simple
grep -oP '[\d]{3}[\s.-][\d]{3}[\s.-][\d]{4}' phones.txt

regex - grouping

Group implementation
grep -oP '([\d]{3}[\s|\.|\-]){2}[\d]{4}' phones.txt

regex - extracting all phone numbers

grep -oP '(\(?[\d]{3}\)?[\s|\.|\-]){2}[\d]{4}' phones.txt

regex - Build and test

Build and test your regex at : regex101