Skip to content

Commit 856aca5

Browse files
committed
Update 9-regular-expressions\10-regexp-greedy-and-lazy\article.md
1 parent b869dfb commit 856aca5

File tree

1 file changed

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

1 file changed

+23
-23
lines changed

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

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,15 @@ Il motore della regexp aggiunge quanti più caratteri possibili alla corrisponde
9494

9595
L'obiettivo della nostra esercitazione non è questo, proprio in questi casi viene in soccorso la modalità lazy.
9696

97-
## Modalità lazy
97+
## Modalità lazy (pigra)
9898

9999
La modalità lazy di un quantificatore è l'opposto della modalità greedy. Significa: "ripeti il minor numero di volte".
100100

101101
Possiamo abilitarla mettendo un punto interrogativo `pattern:'?'` dopo il quantificatore, così che diventi `pattern:*?` o `pattern:+?` o ancora `pattern:??` per `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+
Ricapitoliamo per chiarezza: di norma il punto interrogativo `pattern:?` è di per sé un quantificatore (zero o un carattere), ma se aggiunto *dopo un altro quantificatore (anche dopo se stesso)* assume un altro significato: cambia la modalità di corrispondenza da greedy a lazy.
104104

105-
The regexp `pattern:/".+?"/g` works as intended: it finds `match:"witch"` and `match:"broom"`:
105+
La regexp `pattern:/".+?"/g` soddisfa le nostre esigenze: trova `match:"witch"` e `match:"broom"`:
106106

107107
```js run
108108
let regexp = /".+?"/g;
@@ -112,63 +112,63 @@ 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+
Per comprendere distintamente cosa sia cambiato, seguiamo la ricerca passo dopo passo.
116116

117-
1. The first step is the same: it finds the pattern start `pattern:'"'` at the 3rd position:
117+
1. Il primo step non cambia: trova l'inizio del pattern `pattern:'"'` nella terza posizione:
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. Anche il secondo step è simile: il motore trova una corrispondenza per il punto `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. Da questo punto la ricerca procede in modo differente. Dal momento che il quantificatore è in modalità lazy `pattern:+?`, il motore non prova a cercare il punto più di una volta, si ferma e cerca subito la corrispondenza con il resto del pattern `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+
Se ci fosse un doppio apice a questo punto la ricerca sarebbe già terminata, ma c'è una `'i'` e quindi nessuna corrispondenza.
130+
4. Il motore della regexp allora aumenta il numero delle ripetizioni per il punto e riprova:
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+
Ancora nessun risultato. Il numero delle ripetizioni, pertanto, si accresce di volta in volta...
135+
5. ...finché viene riscontrata la corrispondenza con il resto del pattern:
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 ricerca successiva inizia dalla fine della corrispondenza corrente e produce un altro risultato:
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+
In questo esempio abbiamo visto come funziona la modalità lazy per `pattern:+?`. I quantificatori `pattern:*?` e `pattern:??` operano in modo simile: il motore della regexp aumenta il numero delle ripetizioni solo se il resto del pattern non ha corrispondenza in una data posizione.
144144

145-
**Laziness is only enabled for the quantifier with `?`.**
145+
**La modalità lazy è abilitata unicamente per il quantificatore seguito da `?`.**
146146

147-
Other quantifiers remain greedy.
147+
Gli altri quantificatori continuano ad operare in modalità greedy.
148148

149-
For instance:
149+
Per esempio:
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. Il pattern `pattern:\d+` cerca quanti più caratteri gli è possibile (modalità greedy), e si ferma quindi dopo aver trovato `match:123`, perché il carattere successivo è una spaziatura `pattern:' '`.
156+
2. Segue la corrispondenza dello spazio nel pattern.
157+
3. A questo punto c'è `pattern:\d+?`. Il quantificatore è modalità lazy, perciò trova solo una cifra `match:4` e prova a verificare se il resto del pattern è soddisfatto.
158158

159-
...But there's nothing in the pattern after `pattern:\d+?`.
159+
...Nel pattern, tuttavia, non c'è niente dopo `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+
La modalità lazy non ripete nulla se non c'è un motivo. Il pattern è finito e conclude la ricerca. La nostra corrispondenza è `match:123 4`.
162162

163-
```smart header="Optimizations"
163+
```smart header="Ottimizzazioni"
164164
Modern regular expression engines can optimize internal algorithms to work faster. So they may work a bit differently from the described algorithm.
165165
166166
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.
167167
168168
Complex regular expressions are hard to optimize, so the search may work exactly as described as well.
169169
```
170170

171-
## Alternative approach
171+
## Approccio alternativo
172172

173173
With regexps, there's often more than one way to do the same thing.
174174

0 commit comments

Comments
 (0)