Regex I keep forgetting¶
Estimated time to read: 2 minutes
- Last Updated: May, 2024
Finding multiple lines¶
I use this regex to match at least two consecutive empty lines (which contain spaces or tabs) at the start of a string or line.
^
the pattern must start from the beginning of a line in the string
(
starts a capturing group. This allows you to apply a quantifier to the entire sequence within the parentheses
\s*
matches any whitespace character (e.g. spaces, tabs, etc.) 0 or more times. The asterisk *
is a quantifier that means "0 or more of the preceding token."
\r?
optionally matches a carriage return character. The question mark ?
is a quantifier that means "0 or 1 of the preceding token," making the carriage return optional. This part of the pattern is for compatibility with different newline conventions: \r\n
for Windows and \n
for Linux
\n
matches a newline character
)
ends the capturing group
{2,}
is a quantifier that matches the preceding group at least 2 times, but potentially more. This means that the pattern is looking for at least two consecutive lines that may contain whitespace but no other characters