Regular expressions : expressions régulières.
Reconnaître des motifs (pattern match) textuels.
Les expressions décrivant des motifs reconnaissables utilisent :
a, b, 0, _, etc.), reconnu comme
tels*,
+, [, ^, etc.)Pour désigner des caractères normaux ayant la forme de caractères spéciaux, on les précède d'un caractère
d'échappement \ (\*, \+, etc.)
Les regex sont intégrées dans la plate-forme standard depuis Java 1.4.
Les expressions régulières sont utilisées dans nombre de domaines, comme :
mon?Fichier*.*)motclé*)Exemples d'expressions :
<strong>[</strong>ABC<strong>]</strong><br> <strong>[</strong>A<strong>-</strong>Z<strong>]</strong><br> <strong>[</strong>A<strong>-</strong>Za<strong>-</strong>z<strong>]*<br> [</strong>A<strong>-</strong>Za<strong>-</strong>z<strong> ]</strong><strong><br> [^,] </strong>
| Motif | Syntaxe | |
|---|---|---|
| Motifs | Tout caractère |
<strong>.</strong> |
| 0 ou 1 fois un caractère |
c<strong>?</strong> |
|
| Tout caractère parmi un ensemble | <strong>[</strong>ABC<strong>]</strong> |
|
| Tout caractère sauf ceux d'un ensemble | <strong>[^</strong>DEF<strong>]</strong> |
|
| Echappement | <strong>\</strong>. <strong>\</strong>? <strong>\</strong>[
<strong>\</strong>$ <strong>\</strong>^ |
|
| Début de ligne | <strong>^</strong><strong></strong> |
|
| Fin de ligne |
<strong>$</strong><strong></strong> |
|
| Début d'un mot | <strong>\<</strong><strong></strong> |
|
| Fin d'un mot | <strong>\></strong><strong></strong> |
|
| Début ou fin d'un mot | <strong>\b</strong> |
|
| Sauf début ou fin de mot | <strong>\B</strong> |
|
| Répétitions | 0 ou n fois l'element | élément<strong>*</strong> |
| 1 ou n fois l'élément | élément<strong>+</strong> |
|
| n fois l'élément | élément<strong>{</strong>n<strong>}</strong> |
|
| Entre min et max fois l'élément | élément<strong>{</strong>min,max<strong>}</strong> |
|
| Conditions | Ou | <strong>(</strong>élément1<strong>|</strong>élément2<strong>)</strong> |
l'algèbre des ensembles réguliers. Le caractère joker
* est formellement appelé étoile de Kleene.