TutreNews Learn

Regex in practice

Real-world patterns

Goal: Combine all building blocks for dates, e-mail addresses and log lines · 10 min

Real patterns are built from small, tested pieces. A reliable method:

  1. Write 3 texts that must match and 3 that must not.
  2. Build the pattern piece by piece, from left to right.
  3. Test after every piece.
  4. Anchor it when validating.

Date dd.mm.yyyy (format check, not calendar check): ^(0[1-9]|[12]\d|3[01])\.(0[1-9]|1[0-2])\.\d{4}$

  • day: 01-09, 10-29 or 30-31
  • month: 01-09 or 10-12

Simple e-mail check: ^[^\s@]+@[^\s@]+\.[^\s@]+$: no spaces or @ in the name, an @, a domain, a dot, a top-level domain. It accepts some invalid addresses and that is fine: the only real test of an address is to send a message to it.

Log line "2026-12-05 14:03:22 ERROR disk full": ^(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2}) (INFO|WARN|ERROR) (.+)$ captures date, time, level and message.

Regular expressions check format, not meaning: (0[1-9]|[12]\d|3[01]) cannot know that 31.02. does not exist. Do that check in code.

Worked example: Parsing a log line

  1. Line: "2026-12-05 14:03:22 ERROR disk full".
  2. The pattern captures four groups: date, time, level, message.
  3. We print level and message.
const line = "2026-12-05 14:03:22 ERROR disk full";
const m = line.match(/^(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2}) (INFO|WARN|ERROR) (.+)$/);
console.log(m[3] + " -> " + m[4]);

Output:

ERROR -> disk full