Skip to content

Anchors: string start ^ and end $ #48

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
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

The empty string is the only match: it starts and immediately finishes.
L'unica corrispondenza si ha con la stringa vuota: inizia e poi finisce immediatamente.

The task once again demonstrates that anchors are not characters, but tests.
Questo task dimostra ancora che gli ancoraggi non rappresentano caratteri, bensì test.

The string is empty `""`. The engine first matches the `pattern:^` (input start), yes it's there, and then immediately the end `pattern:$`, it's here too. So there's a match.
La stringa è vuota `""`. Il motore prima fa cerca corrispondenze per `pattern:^` (inizio input), ed è presente, e subito dopo cerca la fine `pattern:$`, e c'è anch'essa. Quindi c'è corrispondenza.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Regexp ^$

Which string matches the pattern `pattern:^$`?
Quale stringa corrisponde al pattern `pattern:^$`?
16 changes: 8 additions & 8 deletions 9-regular-expressions/12-regexp-anchors/2-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 `pattern:i` flag is enabled).
Una coppia di cifre esadecimali è `pattern:[0-9a-f]{2}` (assumendo che la flag `pattern:i` sia abilitata).

We need that number `NN`, and then `:NN` repeated 5 times (more numbers);
Abbiamo bisogno di quella coppia `NN`, e dopo `:NN` ripetuto per 5 volte (altre coppie);

The regexp is: `pattern:[0-9a-f]{2}(:[0-9a-f]{2}){5}`
La regexp è: `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:^...$`.
Ora dimostriamo che la corrispondenza catturi tutto il testo: che inizi con l'indirizzo MAC e finisca al suo termine. Otteniamo questo risultato circondando il pattern da `pattern:^...$`.

Finally:
Infine:

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

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

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

alert( reg.test('01:32:54:67:89') ); // false (5 numbers, need 6)
alert( reg.test('01:32:54:67:89') ); // false (5 numeri, devono essere 6)

alert( reg.test('01:32:54:67:89:ZZ') ) // false (ZZ in the end)
alert( reg.test('01:32:54:67:89:ZZ') ) // false (ZZ alla fine)
```
18 changes: 9 additions & 9 deletions 9-regular-expressions/12-regexp-anchors/2-test-mac/task.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# Check MAC-address
# Controllo MAC-address

[MAC-address](https://en.wikipedia.org/wiki/MAC_address) of a network interface consists of 6 two-digit hex numbers separated by a colon.
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.

For instance: `subject:'01:32:54:67:89:AB'`.
Per esempio: `subject:'01:32:54:67:89:AB'`.

Write a regexp that checks whether a string is MAC-address.
Scrivi una regexp che controlli se una stringa sia un MAC-address.

Usage:
Uso:
```js
let reg = /your regexp/;
let reg = /la tua regexp/;

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

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

alert( reg.test('01:32:54:67:89') ); // false (5 numbers, must be 6)
alert( reg.test('01:32:54:67:89') ); // false (5 coppie, devono essere 6)

alert( reg.test('01:32:54:67:89:ZZ') ) // false (ZZ ad the end)
alert( reg.test('01:32:54:67:89:ZZ') ) // false (ZZ alla fine)
```
32 changes: 16 additions & 16 deletions 9-regular-expressions/12-regexp-anchors/article.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# String start ^ and finish $
# Inizio stringa ^ e fine $

The caret `pattern:'^'` and dollar `pattern:'$'` characters have special meaning in a regexp. They are called "anchors".
L'accento circonflesso `pattern:'^'` e il simbolo del dollaro `pattern:'$'` sono caratteri che hanno un significato speciale nelle regexp. Vengono chiamati "ancoraggi" (anchor).

The caret `pattern:^` matches at the beginning of the text, and the dollar `pattern:$` -- in the end.
Il simbolo `pattern:^` trova corrispondenza all'inizio del testo, e il dollaro `pattern:$` la trova alla fine del testo.

For instance, let's test if the text starts with `Mary`:
Per esempio, vediamo se il testo inizia con `Mary`:

```js run
let str1 = "Mary had a little lamb, it's fleece was white as snow";
Expand All @@ -14,13 +14,13 @@ alert( /^Mary/.test(str1) ); // true
alert( /^Mary/.test(str2) ); // false
```

The pattern `pattern:^Mary` means: "the string start and then Mary".
Il pattern `pattern:^Mary` vuol dire: "la stringa inizia e subito dopo c'è Mary".

Now let's test whether the text ends with an email.
Ora verifichiamo se il testo finisce con una email.

To match an email, we can use a regexp `pattern:[-.\w]+@([\w-]+\.)+[\w-]{2,20}`.
Per trovare corrispondenza con un'email, possiamo usare la regexp `pattern:[-.\w]+@([\w-]+\.)+[\w-]{2,20}`.

To test whether the string ends with the email, let's add `pattern:$` to the pattern:
Per testare se la stringa finisca con una email, aggiungiamo `pattern:$` al pattern:

```js run
let reg = /[-.\w]+@([\w-]+\.)+[\w-]{2,20}$/g;
Expand All @@ -32,24 +32,24 @@ alert( reg.test(str1) ); // true
alert( reg.test(str2) ); // false
```

We can use both anchors together to check whether the string exactly follows the pattern. That's often used for validation.
Possiamo utilizzare entrambi gli ancoraggi insieme per controllare che la stringa segua uno specifico pattern. È un metodo usato spesso per la validazione.

For instance we want to check that `str` is exactly a color in the form `#` plus 6 hex digits. The pattern for the color is `pattern:#[0-9a-f]{6}`.
Per esempio vogliamo controllare che `str` sia esattamente un colore nella forma `#` più 6 esadecimali. Il pattern per il colore è `pattern:#[0-9a-f]{6}`.

To check that the *whole string* exactly matches it, we add `pattern:^...$`:
Per verificare che l'*intera stringa* vi corrisponda in modo esatto, aggiungiamo `pattern:^...$`:

```js run
let str = "#abcdef";

alert( /^#[0-9a-f]{6}$/i.test(str) ); // true
```

The regexp engine looks for the text start, then the color, and then immediately the text end. Just what we need.
Il motore delle regexp cerca l'inizio del testo, successivamente il colore, e infine cerca immediatamente la fine del testo. Proprio ciò di cui abbiamo bisogno.

```smart header="Anchors have zero length"
Anchors just like `\b` are tests. They have zero-width.
```smart header="Gli ancoraggi hanno lunghezza zero"
Gli ancoraggi, proprio come `\b`, sono test. Hanno larghezza zero.

In other words, they do not match a character, but rather force the regexp engine to check the condition (text start/end).
In altre parole, non cercano corrispondenze per un carattere, piuttosto forzano il motore delle regexp a cercare la condizione specifica (inizio/fine del testo).
```

The behavior of anchors changes if there's a flag `pattern:m` (multiline mode). We'll explore it in the next chapter.
Il comportamento degli ancoraggi cambia se c'è la flag `pattern:m` (modalità multi linea). L'approfondiremo meglio nel prossimo capitolo.
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