Skip to content

Greedy and lazy quantifiers #318

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

The result is: `match:123 4`.
Il risultato è: `match:123 4`.

First the lazy `pattern:\d+?` tries to take as little digits as it can, but it has to reach the space, so it takes `match:123`.
Inizialmente il quantificatore lazy `pattern:\d+?` prova a prendere il minor numero di cifre, ma deve raggiungere lo spazio, perciò include `match:123`.

Then the second `\d+?` takes only one digit, because that's enough.
Il secondo `\d+?` prende una cifra soltanto perché è sufficiente.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# A match for /d+? d+?/
# Individuate la corrispondenza per /d+? d+?/

What's the match here?
Quale è la corrispondenza in questo caso?

```js
alert( "123 456".match(/\d+? \d+?/g) ); // ?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
We need to find the beginning of the comment `match:<!--`, then everything till the end of `match:-->`.
Abbiamo bisogno di trovare l'inizio del commento `match:<!--`, e dopo tutto quello che c'è fino a `match:-->`.

An acceptable variant is `pattern:<!--.*?-->` -- the lazy quantifier makes the dot stop right before `match:-->`. We also need to add flag `pattern:s` for the dot to include newlines.
Una variante accettabile è `pattern:<!--.*?-->`, il quantificatore lazy fa sì che la ricerca si fermi prima di `match:-->`. Dobbiamo, inoltre, aggiungere il flag `pattern:s` in modo che il punto includa gli a capo.

Otherwise multiline comments won't be found:
In caso contrario i commenti multilinea non verranno trovati:

```js run
let regexp = /<!--.*?-->/gs;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Find HTML comments
# Trovate i commenti HTML

Find all HTML comments in the text:
Trovate tutti i commenti HTML nel testo:

```js
let regexp = /your regexp/g;

let str = `... <!-- My -- comment
test --> .. <!----> ..
test --> .. <!----> ..
`;

alert( str.match(regexp) ); // '<!-- My -- comment \n test -->', '<!---->'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

The solution is `pattern:<[^<>]+>`.
La soluzione è `pattern:<[^<>]+>`.

```js run
let regexp = /<[^<>]+>/g;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Find HTML tags
# Trovate i tag HTML

Create a regular expression to find all (opening and closing) HTML tags with their attributes.
Create un'espressione regolare per trovare tutti i tag HTML (di apertura e di chiusura) con i loro attributi.

An example of use:
Ecco un esempio d'uso:

```js run
let regexp = /your regexp/g;
Expand All @@ -12,4 +12,4 @@ let str = '<> <a href="/"> <input type="radio" checked> <b>';
alert( str.match(regexp) ); // '<a href="/">', '<input type="radio" checked>', '<b>'
```

Here we assume that tag attributes may not contain `<` and `>` (inside quotes too), that simplifies things a bit.
In questo caso presumiamo che gli attributi dei tag non contengano `<` e `>` dentro i doppi apici per semplificare l'esercizio.
212 changes: 106 additions & 106 deletions 9-regular-expressions/10-regexp-greedy-and-lazy/article.md

Large diffs are not rendered by default.