Skip to content

Commit 1dd0965

Browse files
committed
Update 9-regular-expressions\13-regexp-alternation
1 parent aeda744 commit 1dd0965

File tree

1 file changed

+16
-16
lines changed
  • 9-regular-expressions/13-regexp-alternation

1 file changed

+16
-16
lines changed

9-regular-expressions/13-regexp-alternation/article.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Supponiamo di aver bisogno di trovare il nome di un linguaggio di programmazione
88

99
Ecco la regexp corrispondente: `pattern:html|php|java(script)?`.
1010

11-
E ora un esempio d'uso:
11+
Ed ora un esempio d'uso:
1212

1313
```js run
1414
let regexp = /html|php|css|java(script)?/gi;
@@ -31,37 +31,37 @@ Per applicare l'alternanza ad una determinata parte di un pattern, dobbiamo racc
3131
- `pattern:I love HTML|CSS` trova `match:I love HTML` o `match:CSS`.
3232
- `pattern:I love (HTML|CSS)` corrisponde a `match:I love HTML` o `match:I love CSS`.
3333

34-
## Esempio: una regexp per un orario
34+
## Esempio: regexp per un orario
3535

36-
In previous articles there was a task to build a regexp for searching time in the form `hh:mm`, for instance `12:00`. But a simple `pattern:\d\d:\d\d` is too vague. It accepts `25:99` as the time (as 99 minutes match the pattern, but that time is invalid).
36+
Negli articoli precedenti abbiamo effettuato un'esercitazione per realizzare una regexp e trovare un orario nel formato `hh:mm`, ad esempio `12:00`. Un semplice `pattern:\d\d:\d\d`, tuttavia, è troppo impreciso. Accetta un orario come `25:99` (poiché 99 minuti trova corrispondenza nel pattern, ma non è un orario valido).
3737

38-
How can we make a better pattern?
38+
Come possiamo migliorare questo pattern?
3939

40-
We can use more careful matching. First, the hours:
40+
Possiamo cercare una corrispondenza più accurata. Per prima cosa, le ore:
4141

42-
- If the first digit is `0` or `1`, then the next digit can be any: `pattern:[01]\d`.
43-
- Otherwise, if the first digit is `2`, then the next must be `pattern:[0-3]`.
44-
- (no other first digit is allowed)
42+
- Se la prima cifra è `0` o `1`, allora la cifra successiva può essere una qualsiasi: `pattern:[01]\d`.
43+
- Diversamente, se la prima cifra è `2`, la successiva deve essere `pattern:[0-3]`.
44+
- Non può esserci un altro carattere come prima cifra.
4545

46-
We can write both variants in a regexp using alternation: `pattern:[01]\d|2[0-3]`.
46+
Possiamo scrivere entrambe le varianti in una regexp usando l'alternanza: `pattern:[01]\d|2[0-3]`.
4747

48-
Next, minutes must be from `00` to `59`. In the regular expression language that can be written as `pattern:[0-5]\d`: the first digit `0-5`, and then any digit.
48+
I minuti a seguire, essi devono essere compresi in un intervallo tra `00` e `59`. In un'espressione regolare ciò può essere reso come `pattern:[0-5]\d`: la prima cifra `0-5`, quindi un numero qualsiasi.
4949

50-
If we glue hours and minutes together, we get the pattern: `pattern:[01]\d|2[0-3]:[0-5]\d`.
50+
Unendo le ore con i minuti otteniamo il pattern seguente: `pattern:[01]\d|2[0-3]:[0-5]\d`.
5151

52-
We're almost done, but there's a problem. The alternation `pattern:|` now happens to be between `pattern:[01]\d` and `pattern:2[0-3]:[0-5]\d`.
52+
Abbiamo quasi finito, ma c'è ancora un problema. L'alternanza `pattern:|` al momento sembra avvenire tra `pattern:[01]\d` e `pattern:2[0-3]:[0-5]\d`.
5353

54-
That is: minutes are added to the second alternation variant, here's a clear picture:
54+
In altre parole: i minuti sono aggiunti al secondo termine dell'alternanza, ecco una rappresentazione più chiara:
5555

5656
```
5757
[01]\d | 2[0-3]:[0-5]\d
5858
```
5959

60-
That pattern looks for `pattern:[01]\d` or `pattern:2[0-3]:[0-5]\d`.
60+
Questo pattern cerca `pattern:[01]\d` o `pattern:2[0-3]:[0-5]\d`.
6161

62-
But that's wrong, the alternation should only be used in the "hours" part of the regular expression, to allow `pattern:[01]\d` OR `pattern:2[0-3]`. Let's correct that by enclosing "hours" into parentheses: `pattern:([01]\d|2[0-3]):[0-5]\d`.
62+
Non è quello che vogliamo, l'alternanza dovrebbe riguardare solo le ore e consentire `pattern:[01]\d` o `pattern:2[0-3]`. Correggiamo racchiudendo le ore tra parentesi: `pattern:([01]\d|2[0-3]):[0-5]\d`.
6363

64-
The final solution:
64+
Ed ecco la soluzione definitiva:
6565

6666
```js run
6767
let regexp = /([01]\d|2[0-3]):[0-5]\d/g;

0 commit comments

Comments
 (0)