Regular expressions
Table of contents
- Introduction to regex
- Basic syntax
- Quantifiers
- Classes and groups
- Regex with grep
- Regex with sed and awk
- Practical exercises
1 - Introduction to regex
What is a regex?
A regular expression (regex) is a pattern that describes a set of character strings.
Types of regex
| Type | Tools | Syntax |
|---|---|---|
| BRE | grep, sed | Basic, escaping for +, ? |
| ERE | grep -E, awk | Extended, native +, ?, | |
| PCRE | grep -P | Perl-compatible, lookahead |
Where to use regex
grep: search within filessed: text substitutionawk: filtering and processing[[ =~ ]]: tests in Bash
🔝 Back to table of contents
2 - Basic syntax
Literal characters
# Recherche littérale
grep "hello" fichier.txt
Metacharacters
| Character | Meaning |
|---|---|
. | Any character |
^ | Start of line |
$ | End of line |
* | 0 or more of the preceding |
\ | Escape |
Examples
# N'importe quel caractère
grep "h.llo" fichier.txt # hello, hallo, h3llo...
# Début de ligne
grep "^hello" fichier.txt # Lignes commençant par hello
# Fin de ligne
grep "world$" fichier.txt # Lignes finissant par world
# Les deux
grep "^hello$" fichier.txt # Lignes contenant exactement "hello"
# Échapper un métacaractère
grep "\." fichier.txt # Recherche un point littéral
Anchors
# Ligne vide
grep "^$" fichier.txt
# Ligne avec seulement des espaces
grep "^[[:space:]]*$" fichier.txt
# Mot complet
grep "\bword\b" fichier.txt # ERE/PCRE
grep "\<word\>" fichier.txt # BRE
🔝 Back to table of contents
3 - Quantifiers
Quantifiers table
| Quantifier | Meaning | Example |
|---|---|---|
* | 0 or more | ab*c → ac, abc, abbc |
+ | 1 or more | ab+c → abc, abbc (not ac) |
? | 0 or 1 | ab?c → ac, abc |
{n} | Exactly n | a{3} → aaa |
{n,} | n or more | a{2,} → aa, aaa, aaaa... |
{n,m} | Between n and m | a{2,4} → aa, aaa, aaaa |
Examples
# Avec grep -E (ERE)
grep -E "ab+c" fichier.txt # abc, abbc, abbbc
grep -E "ab?c" fichier.txt # ac, abc
grep -E "a{3}" fichier.txt # aaa
grep -E "a{2,4}" fichier.txt # aa, aaa, aaaa
# BRE (avec échappement)
grep "ab\+c" fichier.txt
grep "a\{3\}" fichier.txt
Greedy vs lazy quantifiers
# Gourmand (par défaut): match le plus long
echo "aaaaab" | grep -oE "a+"
# aaaaa
# Paresseux (PCRE seulement): match le plus court
echo "aaaaab" | grep -oP "a+?"
# a (répété 5 fois)
🔝 Back to table of contents
4 - Classes and groups
Character classes
# Un caractère parmi plusieurs
[abc] # a, b, ou c
[a-z] # Lettre minuscule
[A-Z] # Lettre majuscule
[0-9] # Chiffre
[a-zA-Z] # Lettre
[a-zA-Z0-9] # Alphanumérique
# Négation
[^abc] # Tout sauf a, b, c
[^0-9] # Tout sauf chiffre
POSIX classes
| Class | Equivalent |
|---|---|
[[:alpha:]] | [a-zA-Z] |
[[:digit:]] | [0-9] |
[[:alnum:]] | [a-zA-Z0-9] |
[[:space:]] | Space, tab, newline |
[[:lower:]] | [a-z] |
[[:upper:]] | [A-Z] |
[[:punct:]] | Punctuation |
Groups and alternation
# Groupement
grep -E "(ab)+" fichier.txt # ab, abab, ababab
# Alternation (OU)
grep -E "cat|dog" fichier.txt # cat ou dog
grep -E "(red|blue) car" fichier.txt
# Capture pour backreference
echo "hello hello" | sed 's/\(hello\) \1/DOUBLE/'
# DOUBLE
Backreferences
# Trouver les mots doublés
grep -E "\b(\w+)\s+\1\b" fichier.txt
# Remplacer avec sed
echo "hello hello" | sed 's/\([a-z]*\) \1/\1/'
# hello
🔝 Back to table of contents
5 - Regex with grep
Important options
| Option | Description |
|---|---|
-E | Extended regex (ERE) |
-P | Perl regex (PCRE) |
-i | Case insensitive |
-v | Invert (lines without a match) |
-o | Show only the match |
-n | Show line numbers |
-c | Count the matches |
Practical examples
# Email (simplifié)
grep -E "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}" fichier.txt
# Adresse IP
grep -E "\b[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\b" fichier.txt
# Numéro de téléphone (format FR)
grep -E "0[1-9][0-9]{8}" fichier.txt
# URL
grep -E "https?://[a-zA-Z0-9./?=_-]+" fichier.txt
# Lignes avec des erreurs
grep -E "(error|fail|fatal)" -i log.txt
Extract information
# Extraire tous les emails
grep -oE "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}" fichier.txt
# Extraire les IPs
grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" log.txt
# Compter les occurrences
grep -c "ERROR" log.txt
# Fichiers contenant un pattern
grep -l "TODO" *.py
🔝 Back to table of contents
6 - Regex with sed and awk
sed with regex
# Substitution basique
sed 's/[0-9]\+/NUMBER/g' fichier.txt
# Capture et réutilisation
sed 's/\([A-Za-z]*\) \([A-Za-z]*\)/\2 \1/' fichier.txt
# Supprimer les lignes avec pattern
sed '/^#/d' fichier.txt
# Garder seulement les lignes avec pattern
sed -n '/error/p' fichier.txt
awk with regex
# Lignes matchant un pattern
awk '/error/ {print}' log.txt
# Négation
awk '!/^#/ {print}' fichier.txt
# Match sur un champ spécifique
awk '$3 ~ /error/ {print}' log.txt
# Ne match pas
awk '$3 !~ /success/ {print}' log.txt
Regex in Bash
#!/bin/bash
email="[email protected]"
regex="^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"
if [[ $email =~ $regex ]]; then
echo "Email valide"
echo "Match complet: ${BASH_REMATCH[0]}"
else
echo "Email invalide"
fi
Captures in Bash
#!/bin/bash
ligne="Date: 2024-01-15"
if [[ $ligne =~ Date:\ ([0-9]{4})-([0-9]{2})-([0-9]{2}) ]]; then
echo "Année: ${BASH_REMATCH[1]}"
echo "Mois: ${BASH_REMATCH[2]}"
echo "Jour: ${BASH_REMATCH[3]}"
fi
🔝 Back to table of contents
7 - Practical exercises
Exercise 1: Validate an email
# Créez une fonction qui valide un email
Solution
valider_email() {
local email="$1"
local regex="^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"
if [[ $email =~ $regex ]]; then
return 0
else
return 1
fi
}
valider_email "[email protected]" && echo "Valide"
valider_email "invalid" || echo "Invalide"
Exercise 2: Extract IPs
# Extrayez toutes les adresses IP uniques d'un fichier log
Solution
grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" log.txt | sort -u
Quiz
Q1. Which regex matches lines starting with a digit?
Answer
^[0-9] or ^[[:digit:]]
Q2. How do you make a regex case insensitive with grep?
Answer
grep -i "pattern" fichier
Q3. How do you use extended regex with sed?
Answer
sed -E 's/pattern/replacement/g' or sed -r on some systems
🔝 Back to table of contents
Key takeaways
.= any character,^= start,$= end[abc]= class,[^abc]= negation*= 0+,+= 1+,?= 0-1,{n,m}= between n and mgrep -Efor extended regex (ERE)[[ $var =~ regex ]]to test in Bash${BASH_REMATCH[n]}for captures- Always test your regex on sample data!