Language: EN

regex-espacios-y-saltos

Spaces and Line Breaks in Regex

When working with regular expressions, we will encounter spaces and line breaks as they normally are part of almost any text.

So we will need to learn how to manage and deal with them 👇.

Spaces

Spaces are whitespace characters that we use to separate words and other elements in text.

There are several types of spaces in a text, such as:

  • Single space:
  • Tab: \t
  • End of line space: \r (carriage return) and \n (new line)

To match a whitespace, we can use the metacharacter \s, which includes spaces, tabs, and line breaks.

If we want to find all occurrences of a word that is preceded or followed by one or more spaces, we can use the following pattern:

\spalabra\s

This pattern will match any occurrence of the word "palabra" that is surrounded by spaces.

palabra
here space palabra and more spaces
without space beforepalabra and palabradetras

Other symbols related to spaces:

SymbolMatches
\sAny whitespace (space, tab, line break)
\SAny character that is not a whitespace
\tA tab character (tab)

Line Breaks

Line breaks are characters that indicate the end of a line of text. Depending on the operating system, line breaks can be represented in different ways:

  • Unix/Linux: \n
  • Windows: \r\n
  • Mac (old): \r

To match line breaks, we use \n or \r\n, depending on the operating system.

If we want to match lines that contain a specific word, we can use a pattern like the following:

palabra\n

Let’s try it,

with line break palabra
another word without line break

Other symbols related to line breaks:

SymbolMatches
\nA line break
\rA carriage return
\vA vertical line break
\fA form feed

Combining Spaces and Line Breaks

We can combine spaces and line breaks to create more complex patterns.

For example, if we want to find lines that contain a specific word, and that may be surrounded by whitespace or line breaks, we can use:

\s*palabra\s*

This pattern will match the word "palabra" even if there are spaces before or after.