Basis
Syntax
/.../A regex is enclosed between two forward slashes.
\Cancels the special effect of a special character.
|Alternative (or).
^Start of line.
$End of line.
[ ab ]Match one of the characters inside the square brackets (a or b).
[ a-z ]Match on a range of characters.
[^a]Match if the character is not the one that is after the
^
.(myWord)Match the group inside the parenthesis.
.Match any character.
?The preceding element is present 0 or 1 times.
{ 2 }The preceding element is repeated a defined a number of times.
{ 0,3 }The preceding element is repeated between 0 and 3 times.
*The preceding element is repeated between 0 and infinite number of times.
+The preceding element is repeated between 1 and infinite number of times.
Python
import re
MY_REGEX = ".*"
# Cache the regex (usefull if expression is reused multilple times)
re_compiled = re.compile(MY_REGEX)
# Check if a string matches the expression
if re_compiled.match(myString)
print(f"Expression matched for: {myString}")
Useful Regex
IP
IP_REGEX = r"^([0-9]{1,3}\.){3}[0-9]{1,3}$"
ip a show eth0 | grep -oP '(?<=inet\s)\d+(.\d+){3}'Extract IP from
ip a
.