Skip to content

Commit ab6d78a

Browse files
committed
Update 9-regular-expressions\10-regexp-greedy-and-lazy\article.md
1 parent 529de12 commit ab6d78a

File tree

1 file changed

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

1 file changed

+11
-11
lines changed

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Prendiamo ad esempio la seguente attività.
88

99
Abbiamo bisogno di rimpiazzare tutti i doppi apici `"..."` in un testo con le virgolette basse: `«...»`, che sono preferite nella tipografia di molti paesi.
1010

11-
Ad esempio: `"Hello, world"` dovrebbe diventare `«Hello, world»`. Esistono altre virgolette, come `„Witam, świat!”` (in Polonia) o `「你好,世界」` (in Cina). In questo caso, tuttavia, scegliamo `«...»`.
11+
Ad esempio: `"Hello, world"` dovrebbe diventare `«Hello, world»`. Esistono altre virgolette, come `„Witam, świat!”` (in Polonia) o `「你好,世界」` (in Cina), in questo caso, tuttavia, scegliamo `«...»`.
1212

1313
Innanzitutto dobbiamo individuare le stringhe tra doppi apici per poi sostituirli.
1414

@@ -32,25 +32,25 @@ Questo fenomeno può essere descritto così: "l'avidità è la causa di tutti i
3232

3333
## La ricerca in modalità greedy (avida)
3434

35-
To find a match, the regular expression engine uses the following algorithm:
35+
Per trovare un riscontro, il motore dell'espressione regolare usa il seguente algoritmo:
3636

37-
- For every position in the string
38-
- Try to match the pattern at that position.
39-
- If there's no match, go to the next position.
37+
- Per ogni posizione nella stringa
38+
- Prova a trovare un riscontro del pattern in quella posizione.
39+
- Se non c'è un riscontro, passa alla posizione successiva.
4040

41-
These common words do not make it obvious why the regexp fails, so let's elaborate how the search works for the pattern `pattern:".+"`.
41+
Questa procedura generica non ci spiega con evidenza perché l'espressione regolare fallisca, quindi approfondiamo come funziona la ricerca per il pattern `pattern:".+"`.
4242

43-
1. The first pattern character is a quote `pattern:"`.
43+
1. Il primo carattere del pattern è un doppio apice `pattern:"`.
4444

45-
The regular expression engine tries to find it at the zero position of the source string `subject:a "witch" and her "broom" is one`, but there's `subject:a` there, so there's immediately no match.
45+
Il motore dell'espressione regolare prova a trovarlo nella posizione zero della stringa `subject:a "witch" and her "broom" is one`, ma in quel punto trova `subject:a`, pertanto non c'è immediata corrispondenza.
4646

47-
Then it advances: goes to the next positions in the source string and tries to find the first character of the pattern there, fails again, and finally finds the quote at the 3rd position:
47+
Quindi procede: passa alle successive posizioni nella stringa sorgente e prova a trovare lì il primo carattere del pattern, prima fallisce nuovamente, e poi trova finalmente il doppio apice nella terza posizione:
4848

4949
![](witch_greedy1.svg)
5050

51-
2. The quote is detected, and then the engine tries to find a match for the rest of the pattern. It tries to see if the rest of the subject string conforms to `pattern:.+"`.
51+
2. Rilevato il doppio apice, il motore tenta di trovare riscontro per il resto del pattern. Verifica se il resto della stringa sia conforme a `pattern:.+"`.
5252

53-
In our case the next pattern character is `pattern:.` (a dot). It denotes "any character except a newline", so the next string letter `match:'w'` fits:
53+
Nel nostro esempio il successivo carattere del pattern è `pattern:.` (un punto) che indica "qualsiasi carattere tranne una nuova riga". Trova pertanto corrispondenza nel carattere successivo della stringa `match:'w'`:
5454

5555
![](witch_greedy2.svg)
5656

0 commit comments

Comments
 (0)