Skip to content

Capturing groups #320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions 9-regular-expressions/11-regexp-groups/01-test-mac/solution.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
A two-digit hex number is `pattern:[0-9a-f]{2}` (assuming the flag `pattern:i` is set).
Un numero esadecimale a due cifre è `pattern:[0-9a-f]{2}` (dando per scontato che il flag `pattern:i` sia presente).

We need that number `NN`, and then `:NN` repeated 5 times (more numbers);
Dobbiamo trovare quel numero `NN`, seguito da `:NN` ripetuto 5 volte.

The regexp is: `pattern:[0-9a-f]{2}(:[0-9a-f]{2}){5}`
L'espressione regolare è: `pattern:[0-9a-f]{2}(:[0-9a-f]{2}){5}`

Now let's show that the match should capture all the text: start at the beginning and end at the end. That's done by wrapping the pattern in `pattern:^...$`.
Osserviamo, a questo punto, che la corrispondenza dovrebbe catturare tutto il testo: dall'inizio alla fine. A questo scopo racchiudiamo il pattern all'interno di `pattern:^...$`.

Finally:
Quindi:

```js run
let regexp = /^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}$/i;

alert( regexp.test('01:32:54:67:89:AB') ); // true

alert( regexp.test('0132546789AB') ); // false (no colons)
alert( regexp.test('0132546789AB') ); // false (non ci sono i due punti)

alert( regexp.test('01:32:54:67:89') ); // false (5 numbers, need 6)
alert( regexp.test('01:32:54:67:89') ); // false (5 numeri invece di 6)

alert( regexp.test('01:32:54:67:89:ZZ') ) // false (ZZ in the end)
alert( regexp.test('01:32:54:67:89:ZZ') ) // false (ZZ alla fine)
```
2 changes: 1 addition & 1 deletion 9-regular-expressions/11-regexp-groups/01-test-mac/task.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Controllo MAC-address
# Verificate il MAC-address

Il [MAC-address](https://it.wikipedia.org/wiki/Indirizzo_MAC) di un'interfaccia di rete è composto da 6 coppie di cifre esadecimali separati dai due punti.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
A regexp to search 3-digit color `#abc`: `pattern:/#[a-f0-9]{3}/i`.
L'espressione regolare per cercare il codice di un colore di 3 cifre `#abc` è : `pattern:/#[a-f0-9]{3}/i`.

We can add exactly 3 more optional hex digits. We don't need more or less. The color has either 3 or 6 digits.
Possiamo aggiungere esattamente 3 ulteriori cifre esadecimali opzionali. Non abbiamo bisogno di altro. Il codice di un colore è composto da 3 o 6 cifre.

Let's use the quantifier `pattern:{1,2}` for that: we'll have `pattern:/#([a-f0-9]{3}){1,2}/i`.
Usiamo il quantificatore `pattern:{1,2}` a questo scopo: avremo `pattern:/#([a-f0-9]{3}){1,2}/i`.

Here the pattern `pattern:[a-f0-9]{3}` is enclosed in parentheses to apply the quantifier `pattern:{1,2}`.
In questo caso il pattern `pattern:[a-f0-9]{3}` è racchiuso tra parentesi per applicare ad esso il quantificatore `pattern:{1,2}`.

In action:
Eccolo in azione:

```js run
let regexp = /#([a-f0-9]{3}){1,2}/gi;
Expand All @@ -16,7 +16,7 @@ let str = "color: #3f3; background-color: #AA00ef; and: #abcd";
alert( str.match(regexp) ); // #3f3 #AA00ef #abc
```

There's a minor problem here: the pattern found `match:#abc` in `subject:#abcd`. To prevent that we can add `pattern:\b` to the end:
C'è un piccolo problema adesso: il pattern `match:#abc` trovato in `subject:#abcd`. Per evitarlo possiamo aggiungere `pattern:\b` alla fine:

```js run
let regexp = /#([a-f0-9]{3}){1,2}\b/gi;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Find color in the format #abc or #abcdef
# Trovate un colore nel formato #abc o #abcdef

Write a RegExp that matches colors in the format `#abc` or `#abcdef`. That is: `#` followed by 3 or 6 hexadecimal digits.
Scrivete un'espressione regolare che trovi i colori nel formato `#abc` o `#abcdef`. In altre parole: `#` seguito da 3 o 6 cifre esadecimali.

Usage example:
Esempio d'uso:
```js
let regexp = /your regexp/g;

Expand All @@ -11,4 +11,4 @@ let str = "color: #3f3; background-color: #AA00ef; and: #abcd";
alert( str.match(regexp) ); // #3f3 #AA00ef
```

P.S. This should be exactly 3 or 6 hex digits. Values with 4 digits, such as `#abcd`, should not match.
P.S. Dovrebbe trovare esattamente 3 o 6 cifre esadecimali. I valori con 4 cifre, come `#abcd`, non dovrebbero dar luogo a corrispondenza.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
A positive number with an optional decimal part is: `pattern:\d+(\.\d+)?`.
Un numero positivo con una parte decimale opzionale è: `pattern:\d+(\.\d+)?`.

Let's add the optional `pattern:-` in the beginning:
Aggiungiamo all'inizio il segno meno facoltativo `pattern:-`:

```js run
let regexp = /-?\d+(\.\d+)?/g;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Find all numbers
# Trovate tutti i numeri

Write a regexp that looks for all decimal numbers including integer ones, with the floating point and negative ones.
Scrivete un'espressione regolare che cerchi tutti i numeri decimali e interi, con virgola mobile e negativi.

An example of use:
Un esempio d'uso:

```js
let regexp = /your regexp/g;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
A regexp for a number is: `pattern:-?\d+(\.\d+)?`. We created it in the previous task.
L'espressione regolare per un numero è: `pattern:-?\d+(\.\d+)?`. L'abbiamo creata nell'esercizione precedente.

An operator is `pattern:[-+*/]`. The hyphen `pattern:-` goes first in the square brackets, because in the middle it would mean a character range, while we just want a character `-`.
Per trovare un operatore usiamo `pattern:[-+*/]`. Il trattino `pattern:-` va posto all'inizio nelle parentesi quadre, in mezzo significherebbe un intervallo di caratteri, mentre noi vogliamo soltanto il carattere `-`.

The slash `/` should be escaped inside a JavaScript regexp `pattern:/.../`, we'll do that later.
Dovremmo fare l'escape dello slash `/` dentro una regexp JavaScript `pattern:/.../`, lo faremo dopo.

We need a number, an operator, and then another number. And optional spaces between them.
Abbiamo bisogno di un numero, un operatore, e quindi un altro numero. Tra di essi ci possono essere spazi opzionali.

The full regular expression: `pattern:-?\d+(\.\d+)?\s*[-+*/]\s*-?\d+(\.\d+)?`.
Ecco l'intera espressione regolare: `pattern:-?\d+(\.\d+)?\s*[-+*/]\s*-?\d+(\.\d+)?`.

It has 3 parts, with `pattern:\s*` between them:
1. `pattern:-?\d+(\.\d+)?` - the first number,
1. `pattern:[-+*/]` - the operator,
1. `pattern:-?\d+(\.\d+)?` - the second number.
Questa consta di 3 parti, intervallate da `pattern:\s*`:
1. `pattern:-?\d+(\.\d+)?` - il primo numero,
1. `pattern:[-+*/]` - l'operatore,
1. `pattern:-?\d+(\.\d+)?` - il secondo numero.

To make each of these parts a separate element of the result array, let's enclose them in parentheses: `pattern:(-?\d+(\.\d+)?)\s*([-+*/])\s*(-?\d+(\.\d+)?)`.
Per rendere ciascuna di queste parti un elemento separato dell'array di risultati le racchiudiamo tra parentesi: `pattern:(-?\d+(\.\d+)?)\s*([-+*/])\s*(-?\d+(\.\d+)?)`.

In action:
In azione:

```js run
let regexp = /(-?\d+(\.\d+)?)\s*([-+*\/])\s*(-?\d+(\.\d+)?)/;

alert( "1.2 + 12".match(regexp) );
```

The result includes:
Il risultato include:

- `result[0] == "1.2 + 12"` (full match)
- `result[1] == "1.2"` (first group `(-?\d+(\.\d+)?)` -- the first number, including the decimal part)
- `result[2] == ".2"` (second group`(\.\d+)?` -- the first decimal part)
- `result[3] == "+"` (third group `([-+*\/])` -- the operator)
- `result[4] == "12"` (forth group `(-?\d+(\.\d+)?)` -- the second number)
- `result[5] == undefined` (fifth group `(\.\d+)?` -- the last decimal part is absent, so it's undefined)
- `result[0] == "1.2 + 12"` (l'intera corrispondenza)
- `result[1] == "1.2"` (il primo gruppo `(-?\d+(\.\d+)?)`, il primo numero compresa la parte decimale)
- `result[2] == ".2"` (il secondo gruppo`(\.\d+)?`, la prima parte decimale)
- `result[3] == "+"` (il terzo gruppo `([-+*\/])`, l'operatore)
- `result[4] == "12"` (il quarto gruppo `(-?\d+(\.\d+)?)`, il secondo numero)
- `result[5] == undefined` (il quinto gruppo `(\.\d+)?`, l'ultima parte decimale è assente, quindi equivale ad undefined)

We only want the numbers and the operator, without the full match or the decimal parts, so let's "clean" the result a bit.
Il nostro scopo è ottenere i numeri e l'operatore, senza l'intera corrispondenza o le parti decimali, quindi "puliamo" un po' il risultato.

The full match (the arrays first item) can be removed by shifting the array `result.shift()`.
L'intera corrispondenza (il primo elemento dell'array) possiamo rimuoverla con `result.shift()`.

Groups that contain decimal parts (number 2 and 4) `pattern:(.\d+)` can be excluded by adding `pattern:?:` to the beginning: `pattern:(?:\.\d+)?`.
I gruppi che contengono le parti decimali (gli elementi 2 e 4) `pattern:(.\d+)` li escludiamo aggiungendo `pattern:?:` all'inizio: `pattern:(?:\.\d+)?`.

The final solution:
La soluzione finale:

```js run
function parse(expr) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# Parse an expression
# Analizzate un'espressione

An arithmetical expression consists of 2 numbers and an operator between them, for instance:
Un'espressione aritmetica consiste in 2 numeri e un operatore tra di essi, ad esempio:

- `1 + 2`
- `1.2 * 3.4`
- `-3 / -6`
- `-2 - 2`

The operator is one of: `"+"`, `"-"`, `"*"` or `"/"`.
L'operatore è uno fra: `"+"`, `"-"`, `"*"` o `"/"`.

There may be extra spaces at the beginning, at the end or between the parts.
Potrebbero esserci ulteriori spazi all'inizio, alla fine o tra gli elementi.

Create a function `parse(expr)` that takes an expression and returns an array of 3 items:
Create una funzione `parse(expr)` che riceva un'espressione e restituisca un array di 3 elementi:

1. The first number.
2. The operator.
3. The second number.
1. Il primo numero.
2. L'operatore.
3. Il secondo numero.

For example:
Ad esempio:

```js
let [a, op, b] = parse("1.2 * 3.4");
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy