Skip to content

Commit 19b1283

Browse files
authored
Merge pull request #358 from XyoloJR/groups
Capturing groups
2 parents e036f57 + 60e2dec commit 19b1283

File tree

9 files changed

+173
-173
lines changed

9 files changed

+173
-173
lines changed
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
A two-digit hex number is `pattern:[0-9a-f]{2}` (assuming the flag `pattern:i` is set).
1+
Un nombre hexadécimal à deux chiffres correspond à `pattern:[0-9a-f]{2}` (avec le marqueur `pattern:i`).
22

3-
We need that number `NN`, and then `:NN` repeated 5 times (more numbers);
3+
Nous avons besoin de ce nombre `NN`, et ensuite `:NN` répété 5 fois (pour les autres nombres) ;
44

5-
The regexp is: `pattern:[0-9a-f]{2}(:[0-9a-f]{2}){5}`
5+
L'expression régulière est : `pattern:[0-9a-f]{2}(:[0-9a-f]{2}){5}`
66

7-
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:^...$`.
7+
Montrons maintenant que la correspondance se fait bien sur l'ensemble du texte : commence dès le début de la chaîne testée et termine à la fin. Cela se fait en entourant le motif de `pattern:^...$`.
88

9-
Finally:
9+
Finalement :
1010

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

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

16-
alert( regexp.test('0132546789AB') ); // false (no colons)
16+
alert( regexp.test('0132546789AB') ); // false (pas de double point)
1717

18-
alert( regexp.test('01:32:54:67:89') ); // false (5 numbers, need 6)
18+
alert( regexp.test('01:32:54:67:89') ); // false (5 nombres, au lieu de 6)
1919

20-
alert( regexp.test('01:32:54:67:89:ZZ') ) // false (ZZ in the end)
20+
alert( regexp.test('01:32:54:67:89:ZZ') ) // false (ZZ à la fin)
2121
```
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
# Check MAC-address
1+
# Vérification d'adresse MAC
22

3-
[MAC-address](https://en.wikipedia.org/wiki/MAC_address) of a network interface consists of 6 two-digit hex numbers separated by a colon.
3+
L'[addresse MAC](https://fr.wikipedia.org/wiki/Adresse_MAC) d'une interface réseau est constitué de 6 paires de nombres hexadécimaux séparées par un double point.
44

5-
For instance: `subject:'01:32:54:67:89:AB'`.
5+
Par exemple : `subject:'01:32:54:67:89:AB'`.
66

7-
Write a regexp that checks whether a string is MAC-address.
7+
Écrire une regexp qui vérifie qu'une chaîne de caractères soit bien une adresse MAC.
88

9-
Usage:
9+
Utilisation:
1010
```js
1111
let regexp = /your regexp/;
1212

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

15-
alert( regexp.test('0132546789AB') ); // false (no colons)
15+
alert( regexp.test('0132546789AB') ); // false (double point manquant)
1616

17-
alert( regexp.test('01:32:54:67:89') ); // false (5 numbers, must be 6)
17+
alert( regexp.test('01:32:54:67:89') ); // false (5 paires, mais 6 attendues)
1818

19-
alert( regexp.test('01:32:54:67:89:ZZ') ) // false (ZZ at the end)
19+
alert( regexp.test('01:32:54:67:89:ZZ') ) // false (ZZ à la fin)
2020
```

9-regular-expressions/11-regexp-groups/02-find-webcolor-3-or-6/solution.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
A regexp to search 3-digit color `#abc`: `pattern:/#[a-f0-9]{3}/i`.
1+
Une regexp pour chercher une couleur à trois chiffres `#abc`: `pattern:/#[a-f0-9]{3}/i`.
22

3-
We can add exactly 3 more optional hex digits. We don't need more or less. The color has either 3 or 6 digits.
3+
Nous pouvons y ajouter les 3 autres chiffres optionnels. Nous n'avons pas besoin de plus ou moins. La couleur a soit 3 ou 6 chiffres.
44

5-
Let's use the quantifier `pattern:{1,2}` for that: we'll have `pattern:/#([a-f0-9]{3}){1,2}/i`.
5+
Utilisons le quantificateur `pattern:{1,2}` pour obtenir `pattern:/#([a-f0-9]{3}){1,2}/i`.
66

7-
Here the pattern `pattern:[a-f0-9]{3}` is enclosed in parentheses to apply the quantifier `pattern:{1,2}`.
7+
Ici le schéma `pattern:[a-f0-9]{3}` est entouré de parenthèses pour lui appliquer le quantificateur `pattern:{1,2}`.
88

9-
In action:
9+
En pratique :
1010

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

19-
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:
19+
Il reste un petit problème ici : car ce schéma trouve `match:#abc` dans `subject:#abcd`. Pour éviter cela nous pouvons ajouter à la fin `pattern:\b` :
2020

2121
```js run
2222
let regexp = /#([a-f0-9]{3}){1,2}\b/gi;
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# Find color in the format #abc or #abcdef
1+
# Trouver des couleurs au format #abc ou #abcdef
22

3-
Write a RegExp that matches colors in the format `#abc` or `#abcdef`. That is: `#` followed by 3 or 6 hexadecimal digits.
3+
Écrire une RegExp qui correspond à des couleurs au format `#abc` ou `#abcdef`. C'est à dire : `#` suivi par 3 ou 6 chiffres hexadécimaux.
44

5-
Usage example:
5+
Exemple d'utilisation :
66
```js
77
let regexp = /your regexp/g;
88

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

14-
P.S. This should be exactly 3 or 6 hex digits. Values with 4 digits, such as `#abcd`, should not match.
14+
P.S. Cela doit être exactement 3 ou 6 chiffres. Des valeurs avec 4 chiffres, comme `#abcd`, ne doivent pas ressortir.

9-regular-expressions/11-regexp-groups/03-find-decimal-numbers/solution.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
A positive number with an optional decimal part is: `pattern:\d+(\.\d+)?`.
1+
Un nombre positif avec une éventuelle partie décimale correspond à : `pattern:\d+(\.\d+)?`.
22

3-
Let's add the optional `pattern:-` in the beginning:
3+
Ajoutons l'option `pattern:-` au début :
44

55
```js run
66
let regexp = /-?\d+(\.\d+)?/g;

9-regular-expressions/11-regexp-groups/03-find-decimal-numbers/task.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# Find all numbers
1+
# Trouvez tous les nombres
22

3-
Write a regexp that looks for all decimal numbers including integer ones, with the floating point and negative ones.
3+
Écrire un regexp qui cherche tous les nombres décimaux, comprenant les entiers, les nombres décimaux avec le point comme séparateur et les nombres négatifs.
44

5-
An example of use:
5+
Un exemple d'utilisation :
66

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

9-regular-expressions/11-regexp-groups/04-parse-expression/solution.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
1-
A regexp for a number is: `pattern:-?\d+(\.\d+)?`. We created it in the previous task.
1+
Une regexp pour un nombre : `pattern:-?\d+(\.\d+)?`. Nous l'avons vu dans l'exercice précédent.
22

3-
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 `-`.
3+
Pour l'opérateur `pattern:[-+*/]`. Le tiret `pattern:-` est en premier, car il pourrait signifier un intervalle de caractère, alors que nous souhaitons juste le caractère `-`.
44

5-
The slash `/` should be escaped inside a JavaScript regexp `pattern:/.../`, we'll do that later.
5+
Le slash `/` doit être échappé en javascript dans une regexp `pattern:/.../`, et nous le ferons plus tard.
66

7-
We need a number, an operator, and then another number. And optional spaces between them.
7+
Nous cherchons un nombre, un opérateur puis un autre nombre. Et d'éventuels espaces entre eux.
88

9-
The full regular expression: `pattern:-?\d+(\.\d+)?\s*[-+*/]\s*-?\d+(\.\d+)?`.
9+
Cela done l'expression régulière : `pattern:-?\d+(\.\d+)?\s*[-+*/]\s*-?\d+(\.\d+)?`.
1010

11-
It has 3 parts, with `pattern:\s*` between them:
12-
1. `pattern:-?\d+(\.\d+)?` - the first number,
13-
1. `pattern:[-+*/]` - the operator,
14-
1. `pattern:-?\d+(\.\d+)?` - the second number.
11+
Il y a trois parties, avec `pattern:\s*` entre elles :
12+
1. `pattern:-?\d+(\.\d+)?` - le premier nombre,
13+
1. `pattern:[-+*/]` - l'opérateur,
14+
1. `pattern:-?\d+(\.\d+)?` - le deuxième nombre.
1515

16-
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+)?)`.
16+
Pour faire de chacune de ces parties un élément distinct du tableau de correspondance, entourons-les de parenthèses : `pattern:(-?\d+(\.\d+)?)\s*([-+*/])\s*(-?\d+(\.\d+)?)`.
1717

18-
In action:
18+
Cela donne :
1919

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

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

26-
The result includes:
26+
Le résultat inclus :
2727

28-
- `result[0] == "1.2 + 12"` (full match)
29-
- `result[1] == "1.2"` (first group `(-?\d+(\.\d+)?)` -- the first number, including the decimal part)
30-
- `result[2] == ".2"` (second group`(\.\d+)?` -- the first decimal part)
31-
- `result[3] == "+"` (third group `([-+*\/])` -- the operator)
32-
- `result[4] == "12"` (forth group `(-?\d+(\.\d+)?)` -- the second number)
33-
- `result[5] == undefined` (fifth group `(\.\d+)?` -- the last decimal part is absent, so it's undefined)
28+
- `result[0] == "1.2 + 12"` (la correspondance complète)
29+
- `result[1] == "1.2"` (premier groupe `(-?\d+(\.\d+)?)` -- le premier nombre, avec la partie décimale)
30+
- `result[2] == ".2"` (second groupe`(\.\d+)?` -- la première partie décimale)
31+
- `result[3] == "+"` (troisième groupe `([-+*\/])` -- l'opérateur)
32+
- `result[4] == "12"` (quatrième groupe `(-?\d+(\.\d+)?)` -- le second nombre)
33+
- `result[5] == undefined` (cinquième groupe `(\.\d+)?` -- la deuxième partie décimale est absente, c'est non défini)
3434

35-
We only want the numbers and the operator, without the full match or the decimal parts, so let's "clean" the result a bit.
35+
Nous ne souhaitons que les nombres et l'opérateur, sans la correspondance entière, ni les parties décimales. Faisons alors un peu le ménage.
3636

37-
The full match (the arrays first item) can be removed by shifting the array `result.shift()`.
37+
La correspondance complète(le premier élément du tableau) peut être enlevée par `result.shift()`.
3838

39-
Groups that contain decimal parts (number 2 and 4) `pattern:(.\d+)` can be excluded by adding `pattern:?:` to the beginning: `pattern:(?:\.\d+)?`.
39+
Les groupes contenant les parties décimales(groupes 2 et 4) `pattern:(.\d+)` peuvent être exclus en ajoutant `pattern:?:` au début : `pattern:(?:\.\d+)?`.
4040

41-
The final solution:
41+
La solution complète :
4242

4343
```js run
4444
function parse(expr) {

9-regular-expressions/11-regexp-groups/04-parse-expression/task.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
# Parse an expression
1+
# Parsez une expression
22

3-
An arithmetical expression consists of 2 numbers and an operator between them, for instance:
3+
Une expression arithmétique consiste en 2 nombres et un opérateur entre les deux, par exemple :
44

55
- `1 + 2`
66
- `1.2 * 3.4`
77
- `-3 / -6`
88
- `-2 - 2`
99

10-
The operator is one of: `"+"`, `"-"`, `"*"` or `"/"`.
10+
L'opérateur l'un des : `"+"`, `"-"`, `"*"` ou `"/"`.
1111

12-
There may be extra spaces at the beginning, at the end or between the parts.
12+
Il peut y avoir des espaces supplémentaires au début, à la fin ou entre chaque partie.
1313

14-
Create a function `parse(expr)` that takes an expression and returns an array of 3 items:
14+
Créez une fonction `parse(expr)` qui prend une expression et retourne un tableau de trois éléments :
1515

16-
1. The first number.
17-
2. The operator.
18-
3. The second number.
16+
1. Le premier nombre.
17+
2. L'opérateur.
18+
3. Le second nombre.
1919

20-
For example:
20+
Par exemple :
2121

2222
```js
2323
let [a, op, b] = parse("1.2 * 3.4");

0 commit comments

Comments
 (0)