Salt la conținutul principal
eLearner.app
Modulul 1 · Lecția 3 din 43/32 în curs~8 min
Lecții din modul (3/4)

Wildcard: punctul `.`

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

Exercițiu#regex.m1.l3.e1
Încercări: 0Se încarcă…

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

Se încarcă editorul...
Afișează indiciu

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

Soluție disponibilă după 3 încercări

Review exercise

Exercițiu#regex.m1.l3.e2
Încercări: 0Se încarcă…

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).

Se încarcă editorul...
Afișează indiciu

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

Soluție disponibilă după 3 încercări

Additional challenge

Exercițiu#regex.m1.l3.e3
Încercări: 0Se încarcă…

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

Se încarcă editorul...
Afișează indiciu

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

Soluție disponibilă după 3 încercări