Skip to content

Alternation (OR) | #323

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 5 commits into from
Jun 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Update 9-regular-expressions\13-regexp-alternation
  • Loading branch information
marcellosurdi committed Jun 1, 2021
commit 3f6c5c5f36ab59689eec5e77a2881dc0ac6b8554
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

The first idea can be to list the languages with `|` in-between.
La prima idea potrebbe essere elencare i linguaggi separati da `|`.

But that doesn't work right:
Ma non funziona bene:

```js run
let regexp = /Java|JavaScript|PHP|C|C\+\+/g;
Expand All @@ -11,18 +11,18 @@ let str = "Java, JavaScript, PHP, C, C++";
alert( str.match(regexp) ); // Java,Java,PHP,C,C
```

The regular expression engine looks for alternations one-by-one. That is: first it checks if we have `match:Java`, otherwise -- looks for `match:JavaScript` and so on.
L'interprete dell'espressione regolare cerca le alternanze una per una. In altre parole: per prima cosa cerca `match:Java`, se non la trova cerca `match:JavaScript` e così via.

As a result, `match:JavaScript` can never be found, just because `match:Java` is checked first.
Il risultato è che `match:JavaScript` non trova mai corrispondenza proprio perché `match:Java` viene controllato per prima.

The same with `match:C` and `match:C++`.
Lo stesso accade con `match:C` e `match:C++`.

There are two solutions for that problem:
Ci sono due soluzioni per questo problema:

1. Change the order to check the longer match first: `pattern:JavaScript|Java|C\+\+|C|PHP`.
2. Merge variants with the same start: `pattern:Java(Script)?|C(\+\+)?|PHP`.
1. Cambiare l'ordine di verifica mettendo per primo il termine più lungo: `pattern:JavaScript|Java|C\+\+|C|PHP`.
2. Unire le varianti che cominciano allo stesso modo: `pattern:Java(Script)?|C(\+\+)?|PHP`.

In action:
In azione:

```js run
let regexp = /Java(Script)?|C(\+\+)?|PHP/g;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Find programming languages
# Trovate il nome dei linguaggi di programmazione

There are many programming languages, for instance Java, JavaScript, PHP, C, C++.
Ci sono molti linguaggi di programmazione, Per esempio Java, JavaScript, PHP, C, C++.

Create a regexp that finds them in the string `subject:Java JavaScript PHP C++ C`:
Create una regexp che li trovi nella stringa `subject:Java JavaScript PHP C++ C`:

```js
let regexp = /your regexp/g;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

Opening tag is `pattern:\[(b|url|quote)\]`.
Il tag di apertura è `pattern:\[(b|url|quote)\]`.

Then to find everything till the closing tag -- let's use the pattern `pattern:.*?` with flag `pattern:s` to match any character including the newline and then add a backreference to the closing tag.
Successivamente per trovare tutto fino al tag di chiusura usiamo il pattern `pattern:.*?` con il flag `pattern:s` per cercare la corrispondenza con ogni carattere inclusa una nuova riga. Per concludere aggiungiamo un riferimento all'indietro per il tag di chiusura.

The full pattern: `pattern:\[(b|url|quote)\].*?\[/\1\]`.
L'intero pattern risultante è: `pattern:\[(b|url|quote)\].*?\[/\1\]`.

In action:
In azione:

```js run
let regexp = /\[(b|url|quote)\].*?\[\/\1\]/gs;
Expand All @@ -20,4 +20,4 @@ let str = `
alert( str.match(regexp) ); // [b]hello![/b],[quote][url]http://google.com[/url][/quote]
```

Please note that besides escaping `pattern:[` and `pattern:]`, we had to escape a slash for the closing tag `pattern:[\/\1]`, because normally the slash closes the pattern.
Si noti che oltre l'escape di `pattern:[` e `pattern:]`, abbiamo dovuto fare l'escape dello slash del tag di chiusura `pattern:[\/\1]`, poiché normalmente lo slash termina il pattern.
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
# Find bbtag pairs
# Trovate le coppie di bbtag

A "bb-tag" looks like `[tag]...[/tag]`, where `tag` is one of: `b`, `url` or `quote`.
Un "bb-tag" si presenta così `[tag]...[/tag]`, in cui `tag` è uno tra: `b`, `url` o `quote`.

For instance:
Ad esempio:
```
[b]text[/b]
[url]http://google.com[/url]
```

BB-tags can be nested. But a tag can't be nested into itself, for instance:
I BB-tags possono essere annidati. Un tag, tuttavia, non può essere contenuto all'interno di uno dello stesso tipo, ad esempio:

```
Normal:
Normale:
[url] [b]http://google.com[/b] [/url]
[quote] [b]text[/b] [/quote]

Can't happen:
Non può verificarsi:
[b][b]text[/b][/b]
```

Tags can contain line breaks, that's normal:
I tag possono contenere interruzioni di linea, questo è del tutto normale:

```
[quote]
[b]text[/b]
[/quote]
```

Create a regexp to find all BB-tags with their contents.
Create una regexp per trovare tutti i BB-tags con il loro contenuto.

For instance:
Per esempio:

```js
let regexp = /your regexp/flags;
Expand All @@ -38,7 +38,7 @@ let str = "..[url]http://google.com[/url]..";
alert( str.match(regexp) ); // [url]http://google.com[/url]
```

If tags are nested, then we need the outer tag (if we want we can continue the search in its content):
In caso di tag annidati ci occorre il tag esterno (se lo desideriamo possiamo continuare la ricerca nel contenuto appena ricavato):

```js
let regexp = /your regexp/flags;
Expand Down