Skip to main content
Module 3 · Lesson 4 of 412/32 in the course~16 min
Module lessons (4/4)

Empty lines and trim

Lesson preview5 hands-on exercises readyRegex Course · Module 3 — Anchors and boundaries

Useful patterns: matching empty lines, trimming whitespace at the start/end of a line.

What you'll learn in this lesson

  • Locate completely empty rows using the zero-width ^$ idiom with the gm flag.
  • Distinguish between totally empty lines (^$) e righe contenenti solo whitespace invisibili (^\s*$).
  • Capture superfluous spaces at the beginning (^\s+) and end of the line (\s+$) to implement trim operations.
  • Avoid partial lines by writing correct anchored patterns to filter comments (^#.* with gm).

Overview

By combining the anchors ^ and $ con la flag m otteniamo tre idiomi essenziali per pulire dati, file di configurazione e testi sporchi: rilevare righe vuote (^$), detect white lines with invisible spaces (^\s*$) ed eseguire il trim per singola riga (^\s+ / \s+$).

Sections covered in this lesson

  1. 1Mental model: empty lines vs. spaces at the margins
  2. 2Progressive examples
  3. 3Guided laboratory
  4. 4Check for understanding
Pro Lesson · Module 3 — Anchors and boundaries

Unlock "Empty lines and trim" and the full Regex Course curriculum

Useful patterns: matching empty lines, trimming whitespace at the start/end of a line.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in Regex Course plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.