Skip to content

Commit af5e7f7

Browse files
committed
lazy part, first pass
1 parent 936d59b commit af5e7f7

File tree

1 file changed

+27
-27
lines changed
  • 9-regular-expressions/10-regexp-greedy-and-lazy

1 file changed

+27
-27
lines changed

9-regular-expressions/10-regexp-greedy-and-lazy/article.md

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,15 @@ Le moteur d'expression régulière ajoute autant de caractères que possible pou
9494

9595
Nous avons besoin d'autre chose pour mener à bien notre tâche. Et le mode paresseux va pouvoir nous aider.
9696

97-
## Lazy mode
97+
## Mode paresseux
9898

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".
100100

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:'?'`.
102102

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.
104104

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"`:
106106

107107
```js run
108108
let regexp = /".+?"/g;
@@ -112,60 +112,60 @@ let str = 'a "witch" and her "broom" is one';
112112
alert( str.match(regexp) ); // "witch", "broom"
113113
```
114114

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.
116116

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 :
118118

119119
![](witch_greedy1.svg)
120120

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:'.'`:
122122

123123
![](witch_greedy2.svg)
124124

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:'"'` :
126126

127127
![](witch_lazy3.svg)
128128

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 :
131131

132132
![](witch_lazy4.svg)
133133

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 :
136136

137137
![](witch_lazy5.svg)
138138

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 :
140140

141141
![](witch_lazy6.svg)
142142

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.
144144

145-
**Laziness is only enabled for the quantifier with `?`.**
145+
**La paresse n'est active que pour le quantificateur suivi de `?`.**
146146

147-
Other quantifiers remain greedy.
147+
Les autres quantificateurs restent glouton.
148148

149-
For instance:
149+
Par exemple :
150150

151151
```js run
152152
alert( "123 456".match(/\d+ \d+?/) ); // 123 4
153153
```
154154

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.
158158

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+?`.
160160

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`.
162162

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.
165165
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.
167167
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.
169169
```
170170

171171
## Alternative approach

0 commit comments

Comments
 (0)