Direct naar de hoofdinhoud
eLearner.app
Module 1 · Les 3 van 43/32 in de cursus~8 min
Modulelessen (3/4)

Het jokerteken: de punt `.`

The dot . in regex is the wildcard: it matches any single character\u2026 with one important exception: it does NOT match the newline (\n).

Code
Pattern: c.t
Sample:  cat cot cut c@t c\nt
         ^^^ ^^^ ^^^ ^^^

Three letters: a c, any character, a t. No newline in between, so c\nt is not matched (by default).

The dot is extremely useful but also dangerous: used without discipline it captures more than you intended. Combined with the quantifiers in module 2 (.*, .+?) it is the source of 90% of patterns that "don't work the way I expected".

The s flag: "dotAll"

With the s flag (also called dotAll or single-line) the dot matches every character, newline included. Useful to extract blocks that span multiple lines.

Code
Pattern: <p>.*</p>
Flag:    gs
Sample:  <p>prima\nseconda</p>
         ^^^^^^^^^^^^^^^^^^^^^

Limits and behavior of the dot wildcard

The dot . is a powerful wildcard, but by default it does not match newline characters (\\n). If you want the dot to match absolutely everything, including newlines, you must enable the s (dotAll) flag. Be careful when combining the dot with quantifiers (.*), as it tends to consume too much text (greedy behavior).

Try it

Oefening#regex.m1.l3.e1
Pogingen: 0Laden…

Find every triplet of characters delimited by parentheses, e.g. `(abc)`, `(xyz)`. Use the wildcard for the 3 inner characters.

Editor laden…
Toon hint

Three dots for three arbitrary characters. The parentheses are meta-characters: they must be escaped with \\.

Oplossing beschikbaar na 3 pogingen

Review exercise

Oefening#regex.m1.l3.e2
Pogingen: 0Laden…

Extract the block between `[INIZIO]` and `[FINE]`, which can span multiple lines. You will need the `s` flag so the dot also matches newlines, and the 'lazy' version of the quantifier (`.*?`, module 2).

Editor laden…
Toon hint

Without the s flag, the dot stops at end of line: add it. The form .*? (lazy) stops the match at the first [FINE].

Oplossing beschikbaar na 3 pogingen

Additional challenge

Oefening#regex.m1.l3.e3
Pogingen: 0Laden…

Find all 3-character sequences starting with `c` and ending with `t` (e.g. `cat`, `cot`, `c-t`).

Editor laden…
Toon hint

The pattern uses the dot '.' to represent the wildcard middle character.

Oplossing beschikbaar na 3 pogingen