Aide-mémoire Regex

Référence regex interactive avec guide de syntaxe consultable. Testez les motifs avec correspondance en direct. Couvre les classes de caractères, les quantificateurs, les groupes et les drapeaux.

Showing 39 of 39 entries

Character Classes

10 entries
.

Any character

Matches any single character except newline (unless s flag is set)

Pattern: c.tMatches: "cat", "cut", "c3t"
\d

Digit

Matches any digit character [0-9]

Pattern: \d+Matches: "42", "007"
\D

Non-digit

Matches any character that is not a digit

Pattern: \D+Matches: "abc", "foo!"
\w

Word character

Matches any alphanumeric character or underscore [A-Za-z0-9_]

Pattern: \w+Matches: "hello", "foo_42"
\W

Non-word character

Matches any character that is not a word character

Pattern: \W+Matches: " ", "!@#"
\s

Whitespace

Matches any whitespace character: space, tab, newline, carriage return, form feed

Pattern: \s+Matches: " ", "\t\n"
\S

Non-whitespace

Matches any character that is not whitespace

Pattern: \S+Matches: "hello", "word"
[abc]

Character set

Matches any single character listed inside the brackets

Pattern: [aeiou]Matches: "a", "e", "i", "o", "u"
[^abc]

Negated set

Matches any single character NOT listed inside the brackets

Pattern: [^aeiou]Matches: "b", "c", "d" (not vowels)
[a-z]

Character range

Matches any character in the specified Unicode range. Combine ranges: [a-zA-Z0-9]

Pattern: [a-z]+Matches: "hello", "world"

Anchors

4 entries
^

Start of string

Matches the position at the start of the string (or line with m flag)

Pattern: ^HelloMatches: "Hello world" (start only)
$

End of string

Matches the position at the end of the string (or line with m flag)

Pattern: world$Matches: "Hello world" (end only)
\b

Word boundary

Matches the position between a word character and a non-word character

Pattern: \bcat\bMatches: "cat" but not "catfish"
\B

Non-word boundary

Matches any position that is not a word boundary

Pattern: \Bcat\BMatches: "concatenate" (mid-word)

Quantifiers

8 entries
*

Zero or more (greedy)

Matches the preceding element zero or more times, as many as possible

Pattern: ab*cMatches: "ac", "abc", "abbc"
+

One or more (greedy)

Matches the preceding element one or more times, as many as possible

Pattern: ab+cMatches: "abc", "abbc" (not "ac")
?

Zero or one

Matches the preceding element zero or one time (makes it optional)

Pattern: colou?rMatches: "color", "colour"
{n}

Exactly n

Matches the preceding element exactly n times

Pattern: \d{4}Matches: "2024", "1234"
{n,}

n or more

Matches the preceding element n or more times

Pattern: \d{2,}Matches: "12", "999", "1000"
{n,m}

Between n and m

Matches the preceding element between n and m times (inclusive)

Pattern: \d{2,4}Matches: "12", "123", "1234"
*?

Zero or more (lazy)

Matches the preceding element zero or more times, as few as possible

Pattern: <.+?>Matches: "<b>" in "<b>bold</b>"
+?

One or more (lazy)

Matches the preceding element one or more times, as few as possible

Pattern: "[^"]+?"Matches: Shortest quoted string

Groups

6 entries
(abc)

Capturing group

Groups the expression and captures the matched text. Accessible as \1, \2, or match[1]

Pattern: (\d{4})Matches: Captures year in "2024-01-15"
(?:abc)

Non-capturing group

Groups the expression without creating a capture. Use to apply quantifiers without capturing

Pattern: (?:ab)+Matches: "ab", "abab", "ababab"
(?=abc)

Positive lookahead

Asserts that what follows the current position matches the pattern (zero-width)

Pattern: \d+(?= USD)Matches: "100" in "100 USD"
(?!abc)

Negative lookahead

Asserts that what follows does NOT match the pattern (zero-width)

Pattern: \d+(?! USD)Matches: "42" in "42 EUR"
(?<=abc)

Positive lookbehind

Asserts that what precedes the current position matches the pattern (zero-width)

Pattern: (?<=\$)\d+Matches: "100" in "$100"
(?<!abc)

Negative lookbehind

Asserts that what precedes does NOT match the pattern (zero-width)

Pattern: (?<!\$)\d+Matches: "42" but not "$42"

Flags

6 entries
g

Global

Find all matches rather than stopping after the first match

Pattern: /\d+/gMatches: All numbers in a string
i

Ignore case

Makes the pattern case-insensitive

Pattern: /hello/iMatches: "hello", "HELLO", "Hello"
m

Multiline

Makes ^ match the start of each line and $ match the end of each line

Pattern: /^\w+/mMatches: First word of each line
s

DotAll

Makes . match any character including newline characters

Pattern: /a.b/sMatches: "a\nb" across newlines
u

Unicode

Enables full Unicode matching; required for \p{...} property escapes and 4-byte Unicode code points

Pattern: /\p{L}+/uMatches: Any Unicode letter
y

Sticky

Matches only from the position indicated by lastIndex; does not advance past it

Pattern: /\d+/yMatches: Only at exact lastIndex position

Special

5 entries
|

Alternation

Matches either the expression before or after the pipe. Acts like OR

Pattern: cat|dogMatches: "cat" or "dog"
\1

Backreference

Matches the same text that was matched by capturing group 1. \2 for group 2, etc.

Pattern: (\w+)\s\1Matches: "the the", "ha ha"
\n

Newline

Matches a newline character (line feed, LF)

Pattern: \r?\nMatches: Unix or Windows line endings
\t

Tab

Matches a horizontal tab character

Pattern: \t+Matches: Tab-indented lines
\\

Literal backslash

Matches a literal backslash character. Backslash must be escaped as \\

Pattern: C:\\\w+Matches: "C:\Users" in file paths

Quick Test

— paste a pattern and see matches highlighted
//g

Comment utiliser Aide-mémoire Regex

  1. 1Parcourez la syntaxe regex par catégorie.
  2. 2Recherchez des motifs ou des concepts spécifiques.
  3. 3Testez votre regex avec le matcher en direct.
  4. 4Copiez n'importe quel motif pour l'utiliser dans votre code.
ZenovayAnalytics

Analytics pensé pour les fondateurs.

  • Suivi des visiteurs en temps réel
  • Vie privée d'abord, sans bandeau cookies
  • Installé en deux minutes
Découvrir Zenovay

Questions fréquemment posées

Qu'est-ce qu'une expression régulière (regex)?
Une expression régulière est une séquence de caractères qui définit un motif de recherche. Regex est utilisée pour la correspondance de chaînes, la validation, la recherche et remplacement, et l'extraction de texte. Elle est prise en charge dans pratiquement tous les langages de programmation.
Quelles sont les différentes saveurs de regex?
Principales saveurs: PCRE (PHP, Python re), JavaScript (ECMAScript), .NET, Java, POSIX. Elles diffèrent dans les fonctionnalités comme le lookbehind, les groupes nommés et le support Unicode. La regex JavaScript a ajouté des fonctionnalités comme les groupes nommés et lookbehind en ES2018.
Qu'est-ce que la correspondance gourmande par rapport à la correspondance paresseuse?
Les quantificateurs gourmands (*, +, ?) correspondent autant que possible. Les quantificateurs paresseux (non-gourmands) (*?, +?, ??) correspondent aussi peu que possible. Exemple: pour '<b>bold</b>', /<.*>/ correspond à '<b>bold</b>' (gourmand) tandis que /<.*?>/ correspond à '<b>' (paresseux).
Qu'est-ce que lookahead et lookbehind?
Lookahead (?=...) et lookbehind (?<=...) sont des assertions de largeur zéro qui correspondent sans consommer de caractères. Lookahead positif: \d(?=px) correspond aux chiffres suivis de 'px'. Lookbehind négatif: (?<!\$)\d+ correspond aux nombres non précédés par '$'.
Quelles sont les erreurs regex courantes?
Ne pas échapper les caractères spéciaux (. correspond à n'importe quel caractère, pas un point littéral). Backtracking catastrophique avec les quantificateurs imbriqués. Utiliser .* quand un motif plus spécifique fonctionnerait. Ne pas ancrer les motifs (oublier ^ et $). Utiliser regex pour l'analyse HTML/XML au lieu d'un analyseur approprié.