Skip to content

Commit bef68cf

Browse files
authored
Merge pull request #280 from JulianCDC/alternation-OR-|
Alternation (OR) |
2 parents a5c4cde + 3dac2af commit bef68cf

File tree

9 files changed

+83
-83
lines changed

9 files changed

+83
-83
lines changed

9-regular-expressions/13-regexp-alternation/01-find-programming-language/solution.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

2-
The first idea can be to list the languages with `|` in-between.
2+
La première idée peut être de lister les langages avec des `|` entre deux.
33

4-
But that doesn't work right:
4+
Mais cela ne fonctionne pas correctement :
55

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

14-
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.
14+
Le moteur d'expression régulière regarde les alternances une par une. C'est-à-dire : il regarde d'abord si nous avons `match:Java`, sinon il recherche `match:JavaScript` et ainsi de suite.
1515

16-
As a result, `match:JavaScript` can never be found, just because `match:Java` is checked first.
16+
Ainsi, `match:JavaScript` ne peut jamais être trouvé, puisque `match:Java` est vérifié en premier.
1717

18-
The same with `match:C` and `match:C++`.
18+
Pareil pour `match:C` et `match:C++`.
1919

20-
There are two solutions for that problem:
20+
Il y a deux solutions à ce problème :
2121

22-
1. Change the order to check the longer match first: `pattern:JavaScript|Java|C\+\+|C|PHP`.
23-
2. Merge variants with the same start: `pattern:Java(Script)?|C(\+\+)?|PHP`.
22+
1. Changer l'ordre pour vérifier le mot le plus long en premier : `pattern:JavaScript|Java|C\+\+|C|PHP`.
23+
2. Fusionner les mots commençant de la même manière : `pattern:Java(Script)?|C(\+\+)?|PHP`.
2424

25-
In action:
25+
En action :
2626

2727
```js run
2828
let regexp = /Java(Script)?|C(\+\+)?|PHP/g;

9-regular-expressions/13-regexp-alternation/01-find-programming-language/task.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# Find programming languages
1+
# Trouver les langages de programmation
22

3-
There are many programming languages, for instance Java, JavaScript, PHP, C, C++.
3+
Il y a beaucoup de langages de programmation, par exemple Java, JavaScript, PHP, C, C++.
44

5-
Create a regexp that finds them in the string `subject:Java JavaScript PHP C++ C`:
5+
Créez une regexp qui les trouve dans une chaine de caractère `subject:Java JavaScript PHP C++ C` :
66

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

9-regular-expressions/13-regexp-alternation/02-find-matching-bbtags/solution.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11

2-
Opening tag is `pattern:\[(b|url|quote)\]`.
2+
Un tag d'ouverture correspond à `pattern:\[(b|url|quote)\]`.
33

4-
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.
4+
Ensuite pour trouver tout jusqu'au tag de fermeture, utilisons le modèle `pattern:.*?` avec le flag `pattern:s` pour trouver n'importe quel caractère en plus des sauts de ligne, puis ajoutons une référence au tag de fermeture.
55

6-
The full pattern: `pattern:\[(b|url|quote)\].*?\[/\1\]`.
6+
Le modèle : `pattern:\[(b|url|quote)\].*?\[/\1\]`.
77

8-
In action:
8+
En action :
99

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

23-
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.
23+
Veuillez noter qu'en plus de `pattern:[` et `pattern:]`, nous avons dû échapper un slash pour le tag de fermeture `pattern:[\/\1]` puisque normalement un slash ferme le modèle.

9-regular-expressions/13-regexp-alternation/02-find-matching-bbtags/task.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
# Find bbtag pairs
1+
# Trouver les paires de bbtag
22

3-
A "bb-tag" looks like `[tag]...[/tag]`, where `tag` is one of: `b`, `url` or `quote`.
3+
Un "bb-tag" ressemble à `[tag]...[/tag]`, `tag` peut être : `b`, `url` ou `quote`.
44

5-
For instance:
5+
Par exemple :
66
```
77
[b]text[/b]
88
[url]http://google.com[/url]
99
```
1010

11-
BB-tags can be nested. But a tag can't be nested into itself, for instance:
11+
Les BB-tags peuvent être imbriqués. Mais un tag ne peut pas être imbriqué dans lui même, par exemple :
1212

1313
```
1414
Normal:
1515
[url] [b]http://google.com[/b] [/url]
1616
[quote] [b]text[/b] [/quote]
1717
18-
Can't happen:
18+
Ne peut pas arriver:
1919
[b][b]text[/b][/b]
2020
```
2121

22-
Tags can contain line breaks, that's normal:
22+
Les tags peuvent contenir des sauts de ligne, c'est normal :
2323

2424
```
2525
[quote]
2626
[b]text[/b]
2727
[/quote]
2828
```
2929

30-
Create a regexp to find all BB-tags with their contents.
30+
Créez une regexp pour trouver tous les BB-tags avec leur contenu.
3131

32-
For instance:
32+
Par exemple :
3333

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

41-
If tags are nested, then we need the outer tag (if we want we can continue the search in its content):
41+
Si les tags sont imbriqués, alors nous voulons le tag extérieur (si nous voulons nous pouvons continuer la recherche dans le contenu) :
4242

4343
```js
4444
let regexp = /your regexp/flags;

9-regular-expressions/13-regexp-alternation/03-match-quoted-string/solution.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
The solution: `pattern:/"(\\.|[^"\\])*"/g`.
1+
La solution : `pattern:/"(\\.|[^"\\])*"/g`.
22

3-
Step by step:
3+
Etape par etape :
44

5-
- First we look for an opening quote `pattern:"`
6-
- Then if we have a backslash `pattern:\\` (we technically have to double it in the pattern, because it is a special character, so that's a single backslash in fact), then any character is fine after it (a dot).
7-
- Otherwise we take any character except a quote (that would mean the end of the string) and a backslash (to prevent lonely backslashes, the backslash is only used with some other symbol after it): `pattern:[^"\\]`
8-
- ...And so on till the closing quote.
5+
- D'abord nous recherchons une guillemet ouvrante `pattern:"`
6+
- Ensuite si nous avons un antislash `pattern:\\` (puisque c'est un caractère spécial nous devons le doubler, mais dans les faits c'est un unique antislash), alors n'importe quel caractère peut se trouver à sa suite (un point).
7+
- Sinon nous prenons n'importe quel caractère à part une guillemet (cela signifierait la fin de la chaine de caractère) et un antislash (pour empêcher les antislash solitaires, un antislash est seulement utilisé avec un autre symbole après lui): `pattern:[^"\\]`
8+
- ...Et on continue jusqu'à atteindre la guillemet fermante.
99

10-
In action:
10+
En action :
1111

1212
```js run
1313
let regexp = /"(\\.|[^"\\])*"/g;

9-regular-expressions/13-regexp-alternation/03-match-quoted-string/task.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
# Find quoted strings
1+
# Trouver les chaines de caractère
22

3-
Create a regexp to find strings in double quotes `subject:"..."`.
3+
Créer une regexp pour trouver les chaines de caractère entre guillemets doubles `subject:"..."`.
44

5-
The strings should support escaping, the same way as JavaScript strings do. For instance, quotes can be inserted as `subject:\"` a newline as `subject:\n`, and the slash itself as `subject:\\`.
5+
La chaine de caractère devrait supporter l'échappement, comme les chaines de caractère JavaScript. Par exemple, des guillemets peuvent être insérés comme ceci `subject:\"` une nouvelle ligne comme `subject:\n`, et un antislash comme `subject:\\`.
66

77
```js
88
let str = "Just like \"here\".";
99
```
1010

11-
Please note, in particular, that an escaped quote `subject:\"` does not end a string.
11+
Veuillez noter qu'une guillemet échapée `subject:\"` ne termine pas une chaine de caractère.
1212

13-
So we should search from one quote to the other ignoring escaped quotes on the way.
13+
Nous devrions donc chercher une guillemet puis la suivante en ignorant celles échapées.
1414

15-
That's the essential part of the task, otherwise it would be trivial.
15+
C'est la partie essentielle de la tâche, à part cela, cela devrait être simple.
1616

17-
Examples of strings to match:
17+
Exemple de chaine de caractère valides :
1818
```js
19-
.. *!*"test me"*/!* ..
20-
.. *!*"Say \"Hello\"!"*/!* ... (escaped quotes inside)
21-
.. *!*"\\"*/!* .. (double slash inside)
22-
.. *!*"\\ \""*/!* .. (double slash and an escaped quote inside)
19+
.. *!*"test me"*/!* ..
20+
.. *!*"Say \"Hello\"!"*/!* ... (guillemets échapées à l'intérieur)
21+
.. *!*"\\"*/!* .. (double slash à l'intérieur)
22+
.. *!*"\\ \""*/!* .. (double slash et guillemets échapées à l'intérieur)
2323
```
2424
25-
In JavaScript we need to double the slashes to pass them right into the string, like this:
25+
En Javascript nous devons doubler les slash pour les placer dans la chaine de caractère, comme ceci :
2626
2727
```js run
2828
let str = ' .. "test me" .. "Say \\"Hello\\"!" .. "\\\\ \\"" .. ';

9-regular-expressions/13-regexp-alternation/04-match-exact-tag/solution.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11

2-
The pattern start is obvious: `pattern:<style`.
2+
Le début du modèle est évident: `pattern:<style`.
33

4-
...But then we can't simply write `pattern:<style.*?>`, because `match:<styler>` would match it.
4+
...Mais nous ne pouvons pas juste écrire `pattern:<style.*?>` puisque `match:<styler>` y correspondrait.
55

6-
We need either a space after `match:<style` and then optionally something else or the ending `match:>`.
6+
Nous avons besoin soit d'un espace après `match:<style` et après optionellement quelque chose d'autre, ou bien la fin `match:>`.
77

8-
In the regexp language: `pattern:<style(>|\s.*?>)`.
8+
Dans le langage des regexp : `pattern:<style(>|\s.*?>)`.
99

10-
In action:
10+
En action :
1111

1212
```js run
1313
let regexp = /<style(>|\s.*?>)/g;

9-regular-expressions/13-regexp-alternation/04-match-exact-tag/task.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# Find the full tag
1+
# Trouver la balise entière
22

3-
Write a regexp to find the tag `<style...>`. It should match the full tag: it may have no attributes `<style>` or have several of them `<style type="..." id="...">`.
3+
Écrivez une regexp pour trouver la balise `<style...>`. Il devrait trouver la balise en entier: il pourrait ne pas avoir d'attributs `<style>` ou en avoir plusieurs `<style type="..." id="...">`.
44

5-
...But the regexp should not match `<styler>`!
5+
...Mais la regexp ne devrait pas trouver `<styler>`!
66

7-
For instance:
7+
Par exemple:
88

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

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

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
# Alternation (OR) |
1+
# Alternance (OU) |
22

3-
Alternation is the term in regular expression that is actually a simple "OR".
3+
Alternance est le terme d'expression régulière qui représente un "OU".
44

5-
In a regular expression it is denoted with a vertical line character `pattern:|`.
5+
Dans une expression régulière l'alternance est représentée par une barre verticale `pattern:|`.
66

7-
For instance, we need to find programming languages: HTML, PHP, Java or JavaScript.
7+
Par exemple, nous souhaitons trouver les langages de programmation suivants: HTML, PHP, Java ou JavaScript.
88

9-
The corresponding regexp: `pattern:html|php|java(script)?`.
9+
La regexp correspondante : `pattern:html|php|java(script)?`.
1010

11-
A usage example:
11+
Exemple d'utilisation:
1212

1313
```js run
1414
let regexp = /html|php|css|java(script)?/gi;
@@ -18,50 +18,50 @@ let str = "First HTML appeared, then CSS, then JavaScript";
1818
alert( str.match(regexp) ); // 'HTML', 'CSS', 'JavaScript'
1919
```
2020

21-
We already saw a similar thing -- square brackets. They allow to choose between multiple characters, for instance `pattern:gr[ae]y` matches `match:gray` or `match:grey`.
21+
Nous avons déjà vu une chose similaire, les crochets. Ils permettent de choisir entre plusieurs caractères, par exemple `pattern:gr[ae]y` correspond à `match:gray` ou `match:grey`.
2222

23-
Square brackets allow only characters or character classes. Alternation allows any expressions. A regexp `pattern:A|B|C` means one of expressions `A`, `B` or `C`.
23+
Les crochets n'autorisent que les caractères ou les classes de caractère. L'alternance permet n'importe quelle expression. Une regexp `pattern:A|B|C` signifie `A`, `B` ou `C`.
2424

25-
For instance:
25+
Par exemple:
2626

27-
- `pattern:gr(a|e)y` means exactly the same as `pattern:gr[ae]y`.
28-
- `pattern:gra|ey` means `match:gra` or `match:ey`.
27+
- `pattern:gr(a|e)y` signifie la même chose que `pattern:gr[ae]y`.
28+
- `pattern:gra|ey` signifie `match:gra` ou `match:ey`.
2929

30-
To apply alternation to a chosen part of the pattern, we can enclose it in parentheses:
31-
- `pattern:I love HTML|CSS` matches `match:I love HTML` or `match:CSS`.
32-
- `pattern:I love (HTML|CSS)` matches `match:I love HTML` or `match:I love CSS`.
30+
Pour appliquer l'alternance à une partie du modèle nous pouvons l'encadrer entre parenthèses:
31+
- `pattern:I love HTML|CSS` correspond à `match:I love HTML` ou `match:CSS`.
32+
- `pattern:I love (HTML|CSS)` correspond à `match:I love HTML` ou `match:I love CSS`.
3333

34-
## Example: regexp for time
34+
## Exemple: regexp d'un temps
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+
Dans les articles précédents il y avait une tâche qui consistait à construire une regexp pour trouver un temps de la forme `hh:mm`, par exemple `12:00`. Mais un simple modèle `pattern:\d\d:\d\d` est trop vague. Il accepte `25:99` comme temps (puisque "99 minutes" correspond au modèle, mais ce temps est invalide).
3737

38-
How can we make a better pattern?
38+
Comment pouvons-nous créer un meilleur modèle ?
3939

40-
We can use more careful matching. First, the hours:
40+
Nous pouvons utiliser une correspondance plus appropriée. Premièrement, les heures :
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+
- Si le premier chiffre est `0` ou `1`, alors le prochain chiffre peut être: `pattern:[01]\d`.
43+
- Sinon, si le premier chiffre est `2`, alors le prochain doit être `pattern:[0-3]`.
44+
- (aucun autre premier chiffre est autorisé)
4545

46-
We can write both variants in a regexp using alternation: `pattern:[01]\d|2[0-3]`.
46+
Nous pouvons écrire les deux variantes dans une regexp en utilisant l'alternance: `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+
Ensuite, les minutes doivent être entre `00` et `59`. Dans le langage des expression régulières cela peut être écrit `pattern:[0-5]\d`: le premier chiffre `0-5`, puis n'importe quel chiffre.
4949

50-
If we glue hours and minutes together, we get the pattern: `pattern:[01]\d|2[0-3]:[0-5]\d`.
50+
Si nous rejoignons les heures et les minutes ensemble, nous obtenons le modèle: `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+
Nous y sommes presque, mais il y a un problème. L'alternance `pattern:|` se trouve désormais entre `pattern:[01]\d` et `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+
Cela signifie que les minutes sont incluses dans la seconde variante d'alternance, voici un affichage plus clair:
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+
Ce modèle recherche `pattern:[01]\d` ou `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+
Mais c'est incorrect, l'alternance ne devrait être utilisé que pour la partie "heures" de l'expression régulière, pour permettre `pattern:[01]\d` OU `pattern:2[0-3]`. Corrigeons cela en mettant les "heures" entre parenthèses : `pattern:([01]\d|2[0-3]):[0-5]\d`.
6363

64-
The final solution:
64+
La solution finale :
6565

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

0 commit comments

Comments
 (0)