You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 9-regular-expressions/10-regexp-greedy-and-lazy/article.md
+27-27Lines changed: 27 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -94,15 +94,15 @@ Le moteur d'expression régulière ajoute autant de caractères que possible pou
94
94
95
95
Nous avons besoin d'autre chose pour mener à bien notre tâche. Et le mode paresseux va pouvoir nous aider.
96
96
97
-
## Lazy mode
97
+
## Mode paresseux
98
98
99
-
The lazy mode of quantifiers is an opposite to the greedy mode. It means: "repeat minimal number of times".
99
+
Le mode paresseux des quantificateurs est l'opposé du mode glouton. Il signifie : "répète le motif le moins de fois possible".
100
100
101
-
We can enable it by putting a question mark `pattern:'?'`after the quantifier, so that it becomes `pattern:*?`or`pattern:+?`or even`pattern:??`for`pattern:'?'`.
101
+
Nous pouvons l'activer en ajoutant un `pattern:'?'`après le quantificateur, il devient alors `pattern:*?`ou`pattern:+?`ou encore`pattern:??`pour le`pattern:'?'`.
102
102
103
-
To make things clear: usually a question mark `pattern:?`is a quantifier by itself (zero or one), but if added *after another quantifier (or even itself)*it gets another meaning -- it switches the matching mode from greedy to lazy.
103
+
Pour clarifier les choses : le point d'interrogation `pattern:?`est en général un quantificateur (zero ou un), mais si nous l'ajoutons *à la suite d'un autre quantificateur (ou bien lui même)*il prend une autre signification -- il change le mode de correspondance de glouton à paresseux.
104
104
105
-
The regexp `pattern:/".+?"/g`works as intended: it finds`match:"witch"`and`match:"broom"`:
105
+
La regexp `pattern:/".+?"/g`fonctionne alors comme prévu : elle trouve`match:"witch"`et`match:"broom"`:
106
106
107
107
```js run
108
108
let regexp =/".+?"/g;
@@ -112,60 +112,60 @@ let str = 'a "witch" and her "broom" is one';
112
112
alert( str.match(regexp) ); // "witch", "broom"
113
113
```
114
114
115
-
To clearly understand the change, let's trace the search step by step.
115
+
Pour bien comprendre la différence, suivons cette recherche pas à pas.
116
116
117
-
1.The first step is the same: it finds the pattern start`pattern:'"'`at the 3rd position:
117
+
1.La première étape est la même : elle trouve le premier motif`pattern:'"'`en 3e position:
118
118
119
119

120
120
121
-
2.The next step is also similar: the engine finds a match for the dot`pattern:'.'`:
121
+
2.L'étape suivante est aussi semblable : le moteur trouve une correspondance pour le point`pattern:'.'`:
122
122
123
123

124
124
125
-
3.And now the search goes differently. Because we have a lazy mode for `pattern:+?`, the engine doesn't try to match a dot one more time, but stops and tries to match the rest of the pattern `pattern:'"'`right now:
125
+
3.La recherche prend ensuite un chemin different. Comme nous somme en mode paresseux pour `pattern:+?`, le moteur n'essaye pas de faire correspondre à nouveau un point, mais s'arrête et essaye immédiatement de trouver une correspondance à la suite du motif `pattern:'"'` :
126
126
127
127

128
128
129
-
If there were a quote there, then the search would end, but there's `'i'`, so there's no match.
130
-
4.Then the regular expression engine increases the number of repetitions for the dot and tries one more time:
129
+
S'il y avait des guillemets ici, alors la recherche s'arrêterait, mais il y a un `'i'`, donc pas de correspondance.
130
+
4.Le moteur d'expressions régulières augmente alors le nombre de répétitions pour le point et essaye à nouveau :
131
131
132
132

133
133
134
-
Failure again. Then the number of repetitions is increased again and again...
135
-
5. ...Till the match for the rest of the pattern is found:
134
+
Nouvel échec. Le nombre de répétitions augmente alors encore et encore ...
135
+
5. ... Jusqu'à trouver une correspondance à la suite du motif :
136
136
137
137

138
138
139
-
6.The next search starts from the end of the current match and yield one more result:
139
+
6.La recherche suivant commence alors depuis la fin de la correspondance et ressort une autre résultat :
140
140
141
141

142
142
143
-
In this example we saw how the lazy mode works for `pattern:+?`. Quantifiers `pattern:*?`and`pattern:??`work the similar way -- the regexp engine increases the number of repetitions only if the rest of the pattern can't match on the given position.
143
+
Dans cet exemple nous avons vu comment le mode paresseux fonctionne pour `pattern:+?`. Les quantificateurs `pattern:*?`et`pattern:??`fonctionne de la même manière -- le moteur de regexp augmente le nombre de répétitions seulement si le reste du motif ne trouve pas de correspondance à cette position.
144
144
145
-
**Laziness is only enabled for the quantifier with`?`.**
145
+
**La paresse n'est active que pour le quantificateur suivi de`?`.**
146
146
147
-
Other quantifiers remain greedy.
147
+
Les autres quantificateurs restent glouton.
148
148
149
-
For instance:
149
+
Par exemple :
150
150
151
151
```js run
152
152
alert( "123 456".match(/\d+\d+?/) ); // 123 4
153
153
```
154
154
155
-
1.The pattern`pattern:\d+`tries to match as many digits as it can (greedy mode), so it finds`match:123`and stops, because the next character is a space`pattern:' '`.
156
-
2.Then there's a space in the pattern, it matches.
157
-
3.Then there's `pattern:\d+?`. The quantifier is in lazy mode, so it finds one digit`match:4`and tries to check if the rest of the pattern matches from there.
155
+
1.Le motif`pattern:\d+`essaye de trouver autant de chiffres que possible (mode glouton), il trouve donc`match:123`et s'arrête, car le caractère suivant est un`pattern:' '`.
156
+
2.Il y a ensuite un espace dans le motif, il y bien correspondance.
157
+
3.Enfin il y a `pattern:\d+?`. Le quantificateur est en mode paresseux, il trouve donc un chiffre`match:4`et vérifie si la suite du motif trouve une correspondance à partir d'ici.
158
158
159
-
...But there's nothing in the pattern after`pattern:\d+?`.
159
+
... Mais il n'y a plus rien dans le motif après`pattern:\d+?`.
160
160
161
-
The lazy mode doesn't repeat anything without a need. The pattern finished, so we're done. We have a match`match:123 4`.
161
+
Le mode paresseux ne répète rien sans en avoir besoin. Le motif est terminé, donc la recherche aussi. Nous avons une correspondance`match:123 4`.
162
162
163
-
```smart header="Optimizations"
164
-
Modern regular expression engines can optimize internal algorithms to work faster. So they may work a bit differently from the described algorithm.
163
+
```smart header="Optimisations"
164
+
Les moteurs d'expressions régulières récents arrive à optimiser leurs algorithmes internes. Ils fonctionnent donc un peu différemment.
165
165
166
-
But to understand how regular expressions work and to build regular expressions, we don't need to know about that. They are only used internally to optimize things.
166
+
Mais pour comprendre comment fonctionne les expression régulières et contruire des expressions régulière, nous n'avons pas besoin d'en savoir plus. Ils sont seulement utilisé en interne pour optimiser les choses.
167
167
168
-
Complex regular expressions are hard to optimize, so the search may work exactly as described as well.
168
+
Comme les expressions régulières complexes sont sont difficiles à optimiser, la recherche peut se dérouler exactement comme nous l'avons décri.
0 commit comments