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
+23-23Lines changed: 23 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -94,15 +94,15 @@ Il motore della regexp aggiunge quanti più caratteri possibili alla corrisponde
94
94
95
95
L'obiettivo della nostra esercitazione non è questo, proprio in questi casi viene in soccorso la modalità lazy.
96
96
97
-
## Modalità lazy
97
+
## Modalità lazy (pigra)
98
98
99
99
La modalità lazy di un quantificatore è l'opposto della modalità greedy. Significa: "ripeti il minor numero di volte".
100
100
101
101
Possiamo abilitarla mettendo un punto interrogativo `pattern:'?'` dopo il quantificatore, così che diventi `pattern:*?` o `pattern:+?` o ancora `pattern:??` per `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
+
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.
104
104
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"`:
106
106
107
107
```js run
108
108
let regexp =/".+?"/g;
@@ -112,63 +112,63 @@ 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
+
Per comprendere distintamente cosa sia cambiato, seguiamo la ricerca passo dopo passo.
116
116
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:
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.Anche il secondo step è simile: il motore trova una corrispondenza per il punto`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.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:'"'`:
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
+
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:
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
+
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:
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 ricerca successiva inizia dalla fine della corrispondenza corrente e produce un altro risultato:
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
+
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.
144
144
145
-
**Laziness is only enabled for the quantifier with`?`.**
145
+
**La modalità lazy è abilitata unicamente per il quantificatore seguito da`?`.**
146
146
147
-
Other quantifiers remain greedy.
147
+
Gli altri quantificatori continuano ad operare in modalità greedy.
148
148
149
-
For instance:
149
+
Per esempio:
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.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.
158
158
159
-
...But there's nothing in the pattern after`pattern:\d+?`.
159
+
...Nel pattern, tuttavia, non c'è niente dopo`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
+
La modalità lazy non ripete nulla se non c'è un motivo. Il pattern è finito e conclude la ricerca. La nostra corrispondenza è`match:123 4`.
162
162
163
-
```smart header="Optimizations"
163
+
```smart header="Ottimizzazioni"
164
164
Modern regular expression engines can optimize internal algorithms to work faster. So they may work a bit differently from the described algorithm.
165
165
166
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.
167
167
168
168
Complex regular expressions are hard to optimize, so the search may work exactly as described as well.
169
169
```
170
170
171
-
## Alternative approach
171
+
## Approccio alternativo
172
172
173
173
With regexps, there's often more than one way to do the same thing.
0 commit comments