முக்கிய உள்ளடக்கத்திற்குச் செல்லவும்
eLearner.app
தொகுதி 3 · பாடம் 4 இன் 4பாடத்திட்டத்தில் 12/32~10 min
தொகுதி பாடங்கள் (4/4)

வெற்று கோடுகள் மற்றும் டிரிம்

Let's combine anchors + the m flag for two extremely useful idioms: matching empty lines and trimming the whitespace at the start/end of each line.

Empty lines

An empty line is a position where ^ and $ coincide: zero characters between the start and the end of the line.

Code
Pattern: ^$
Flag:    gm
Sample:  uno\n\ndue\n\n\ntre

The match is zero-width: the highlighter will show a marker between the two \n. To also match "empty but with spaces" lines, use ^\s*$.

Trimming a line

To remove whitespace at the start or end of a line, combine an anchor + the \s class + a quantifier:

Code
^\s+    matches whitespace at the start of a line (can then be replaced with the empty string, module 7)
\s+$    matches whitespace at the end of a line

With the gm flags you collect every margin whitespace of every line.

Identifying empty lines and trailing spaces

A pure empty line is matched by ^$ (with the m flag). However, many apparently empty lines contain invisible spaces or tabs. The pattern ^\\s*$ intercepts these lines too, making data cleanup much easier.

Try it

உடற்பயிற்சி#regex.m3.l4.e1
முயற்சிகள்: 0ஏற்றுகிறது…

Find every empty line in the sample (zero characters between two newlines). Use `^$` with the `gm` flags.

எடிட்டரை ஏற்றுகிறது…
குறிப்பைக் காட்டு

An empty line is a position where ^ and $ coincide. The m flag is mandatory.

3 முயற்சிகளுக்குப் பிறகு தீர்வு கிடைக்கும்

Review exercise

உடற்பயிற்சி#regex.m3.l4.e2
முயற்சிகள்: 0ஏற்றுகிறது…

Find the whitespace at the end of every line (the whitespace characters that precede the newline). Use `\\s+$` with the `gm` flags.

எடிட்டரை ஏற்றுகிறது…
குறிப்பைக் காட்டு

\\s+ matches one or more whitespace characters (including tab). With gm you anchor to the end of every line.

3 முயற்சிகளுக்குப் பிறகு தீர்வு கிடைக்கும்

Additional challenge

உடற்பயிற்சி#regex.m3.l4.e3
முயற்சிகள்: 0ஏற்றுகிறது…

Find all lines that start with the `#` character (entire comment lines, ignoring inline comments).

எடிட்டரை ஏற்றுகிறது…
குறிப்பைக் காட்டு

Use ^ at the start of the pattern to indicate that the comment character # must start the line.

3 முயற்சிகளுக்குப் பிறகு தீர்வு கிடைக்கும்