Riferimento completo delle espressioni regolari con sintassi, flag, gruppi, lookahead ed esempi di pattern reali.
Prova nel Tester Regex →| Sintassi | Descrizione |
|---|---|
. | Any character except newline |
\d | Digit [0-9] |
\D | Non-digit [^0-9] |
\w | Word char [a-zA-Z0-9_] |
\W | Non-word character |
\s | Whitespace (space, tab, newline) |
\S | Non-whitespace |
[abc] | One of a, b, or c |
[^abc] | Not a, b, or c |
[a-z] | Character in range a–z |
[a-zA-Z] | Letter (any case) |
\p{L} | Any Unicode letter (u flag) |
| Sintassi | Descrizione |
|---|---|
^ | Start of string (or line with m flag) |
$ | End of string (or line with m flag) |
\b | Word boundary |
\B | Non-word boundary |
\A | Start of string (Python/Java) |
\Z | End of string (Python/Java) |
| Sintassi | Descrizione |
|---|---|
* | 0 or more (greedy) |
+ | 1 or more (greedy) |
? | 0 or 1 (optional) |
{n} | Exactly n times |
{n,} | n or more times |
{n,m} | Between n and m times |
*? | 0 or more (lazy) |
+? | 1 or more (lazy) |
?? | 0 or 1 (lazy) |
| Sintassi | Descrizione |
|---|---|
(abc) | Capturing group |
(?:abc) | Non-capturing group |
(?<name>abc) | Named capturing group |
\1 | Backreference to group 1 |
\k<name> | Named backreference |
(?|...) | Branch reset group (PCRE) |
| Sintassi | Descrizione |
|---|---|
(?=abc) | Positive lookahead — must be followed by abc |
(?!abc) | Negative lookahead — must NOT be followed by abc |
(?<=abc) | Positive lookbehind — must be preceded by abc |
(?<!abc) | Negative lookbehind — must NOT be preceded by abc |
| Flag | Nome | Descrizione |
|---|---|---|
g | Global | Find all matches, not just the first |
i | Case-insensitive | Match regardless of case (A = a) |
m | Multiline | ^ and $ match start/end of each line |
s | Dotall | . matches newline characters too |
u | Unicode | Enable full Unicode support (\p{} classes) |
y | Sticky | Match only at lastIndex position (JS) |
x | Extended | Allow whitespace + comments in pattern (Python/PHP) |
| Schema | Regex | Test |
|---|---|---|
[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,} | Prova nel Tester Regex → | |
| URL | https?:\/\/[\w\-]+(\.[\w\-]+)+[\w\-._~:/?#[\]@!$&'()*+,;=%]* | Prova nel Tester Regex → |
| IPv4 Address | \b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b | Prova nel Tester Regex → |
| Phone (US) | \+?1?[\s.-]?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4} | Prova nel Tester Regex → |
| Date (YYYY-MM-DD) | \d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01]) | Prova nel Tester Regex → |
| Time (HH:MM) | (?:[01]\d|2[0-3]):[0-5]\d | Prova nel Tester Regex → |
| UUID | [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12} | Prova nel Tester Regex → |
| Hex Color | #(?:[0-9a-fA-F]{3}){1,2}\b | Prova nel Tester Regex → |
| Slug | [a-z0-9]+(?:-[a-z0-9]+)* | Prova nel Tester Regex → |
| ZIP Code (US) | \d{5}(?:-\d{4})? | Prova nel Tester Regex → |
| Credit Card | (?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}) | Prova nel Tester Regex → |
| HTML Tag | <([a-zA-Z][a-zA-Z0-9]*)(?:\s[^>]*)?\/?>.*?<\/\1> | Prova nel Tester Regex → |
| Markdown Bold | \*\*([^*]+)\*\* | Prova nel Tester Regex → |
| JWT Token | [A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+ | Prova nel Tester Regex → |
| Semver | \bv?(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[\w.]+)?\b | Prova nel Tester Regex → |
| GitHub Username | (?<=github\.com\/)([a-zA-Z0-9](?:[a-zA-Z0-9-]{0,37}[a-zA-Z0-9])?) | Prova nel Tester Regex → |
| Positive Integer | ^[1-9]\d*$ | Prova nel Tester Regex → |
| Float / Decimal | -?\d+(?:\.\d+)? | Prova nel Tester Regex → |
| Blank Lines | ^\s*$ | Prova nel Tester Regex → |
| Duplicate Words | \b(\w+)\s+\1\b | Prova nel Tester Regex → |
Un'espressione regolare è una sequenza di caratteri che definisce un pattern di ricerca. È usata per la ricerca, il matching e la manipolazione di stringhe. Le regex sono supportate nativamente in JavaScript, Python, Java, PHP, Go e la maggior parte dei linguaggi moderni.
I quantificatori golosi (*, +, {n,m}) corrispondono a quanti più caratteri possibile. I quantificatori pigri (*?, +?, {n,m}?) corrispondono a quanti meno caratteri possibile.
I lookahead (?=...) e lookbehind (?<=...) sono asserzioni a larghezza zero — verificano un pattern senza consumare caratteri. Il lookahead positivo (?=foo) afferma che 'foo' segue. Il lookahead negativo (?!foo) afferma che 'foo' NON segue.
La sintassi di base (classi di caratteri, quantificatori, ancore) è coerente nella maggior parte dei linguaggi. Tuttavia, funzionalità come lookbehind, gruppi nominati e flag specifici variano per motore.