10000 add fr tour regex patterns (#2264) · mlachkar/docs.scala-lang@cd8eeab · GitHub
[go: up one dir, main page]

Skip to content

Commit cd8eeab

Browse files
authored
add fr tour regex patterns (scala#2264)
1 parent 3a66eef commit cd8eeab

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

_fr/tour/regular-expression-patterns.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,61 @@ layout: tour
33
title: Regular Expression Patterns
44
partof: scala-tour
55

6-
num: 13
6+
num: 17
77

88
language: fr
99

1010
next-page: extractor-objects
1111
previous-page: singleton-objects
1212
---
13+
14+
Les expressions régulières sont des chaînes de caractères qui peuvent être utilisées pour trouver des motifs (ou l'absence de motif) dans un texte. Toutes les chaînes de caractères peuvent être converties en expressions régulières en utilisant la méthode `.r`.
15+
16+
```scala mdoc
17+
import scala.util.matching.Regex
18+
19+
val numberPattern: Regex = "[0-9]".r
20+
21+
numberPattern.findFirstMatchIn("awesomepassword") match {
22+
case Some(_) => println("Password OK")
23+
case None => println("Password must contain a number")
24+
}
25+
```
26+
27+
Dans l'exemple ci-dessus, `numberPattern` est une `Regex` (EXpression REGulière) que nous utilisons pour vérifier que le mot de passe contient un nombre.
28+
29+
Vous pouvez aussi faire des recherches de groupes d'expressions régulières en utilisant les parenthèses.
30+
31+
```scala mdoc
32+
import scala.util.matching.Regex
33+
34+
val keyValPattern: Regex = "([0-9a-zA-Z- ]+): ([0-9a-zA-Z-#()/. ]+)".r
35+
36+
val input: String =
37+
"""background-color: #A03300;
38+
|background-image: url(img/header100.png);
39+
|background-position: top center;
40+
|background-repeat: repeat-x;
41+
|background-size: 2160px 108px;
42+
|margin: 0;
43+
|height: 108px;
44+
|width: 100%;""".stripMargin
45+
46+
for (patternMatch <- keyValPattern.findAllMatchIn(input))
47+
println(s"key: ${patternMatch.group(1)} value: ${patternMatch.group(2)}")
48+
```
49+
50+
Ici nous analysons les clefs et les valeurs d'une chaîne de caractère. Chaque correspondance a un groupe de sous-correspondances. Voici le résultat :
51+
52+
```
53+
key: background-color value: #A03300
54+
key: background-image value: url(img/header100.png)
55+
key: background-position value: top center
56+
key: background-repeat value: repeat-x
57+
key: background-size value: 2160px 108px
58+
key: margin value: 0
59+
key: height value: 108px
60+
key: width value: 100
61+
```
62+
63+
Traduit par Antoine Pointeau.

0 commit comments

Comments
 (0)
0