Регулярное выражение пустая строка linux

How to check if a line is blank using regex

The pattern you want is something like this in multiline mode:

  • ^ is the beginning of string anchor.
  • $ is the end of string anchor.
  • \s is the whitespace character class.
  • * is zero-or-more repetition of.

In multiline mode, ^ and $ also match the beginning and end of the line.

References:

A non-regex alternative:

You can also check if a given string line is «blank» (i.e. containing only whitespaces) by trim() -ing it, then checking if the resulting string isEmpty() .

In Java, this would be something like this:

The regex solution can also be simplified without anchors (because of how matches is defined in Java) as follows:

API references

Оцените статью
Adblock
detector