Regex Whitespace & Newline Guide

Master regex space characters, blank characters, and line endings with this complete reference. Click any symbol to try it in the tester above.

Whitespace & Newline Reference Table
MetacharacterWhat it MatchesCommon Use Case
\\sAny whitespace: space, tab, newline, carriage returnClean up input with str.replace(/\s+/g, '')
\\SAny non-whitespace characterExtract words: \S+ matches each word
Exactly one space characterMatch a specific format like first last
\\tHorizontal tabParse TSV (tab-separated values) files
\\nLine feed (Linux/macOS newline)Split text into lines
\r\nCarriage return + line feed (Windows newline)Parse Windows-format log files
\\rCarriage return (classic Mac OS)Legacy file format handling

Regex Syntax Demystified

Decode the most searched regex symbols. Click any symbol to insert it into the tester above.

Click to View Full Syntax Table
SymbolNameWhat it DoesExample
^Caret (Start anchor)Matches the beginning of a line or string^Hello matches "Hello" only at the start
$Dollar (End anchor)Matches the end of a line or stringend$ matches "end" only at the end
?Question markMakes the previous character optional (0 or 1)colou?r matches "color" and "colour"
*AsteriskMatches 0 or more of the previous characterab*c matches "ac", "abc", "abbc"
+PlusMatches 1 or more of the previous characterab+c matches "abc", "abbc" (not "ac")
[^abc]Negated classMatches any character NOT listed inside brackets[^0-9] matches any non-digit
\\bWord boundaryMatches the position between a word char and non-word char\bcat\b matches "cat" but not "scatter"
(...)Capturing groupGroups part of the pattern for extraction(\d+)-(\d+) captures area code and number

Python Regex Cheat Sheet

Quick reference for the most-used re module methods and patterns.

Python re Module Methods
MethodPurposeExample
re.search()Find first match anywhere in stringre.search(r'\d+', 'abc123') # matches "123"
re.match()Match only at the beginning of stringre.match(r'Hello', 'Hello World') # matches
re.findall()Return all matches as a listre.findall(r'\d+', 'a1b22c333') # ['1', '22', '333']
re.sub()Replace matches with a stringre.sub(r'\s+', '-', 'a b c') # "a-b-c"
re.split()Split string by patternre.split(r'[,;]', 'a,b;c') # ['a', 'b', 'c']
PowerShell & Grep Quick Reference
ToolTaskCommand
grepFind errors in log filesgrep -E 'ERROR|WARN.*timeout' app.log
grepExtract IPs from a filegrep -oE '[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+' access.log
grepCase-insensitive searchgrep -i 'error' /var/log/syslog
PowerShellMatch pattern in string"hello 123" -match "\d+"
PowerShellReplace with regex"a b c" -replace "\s+", " "

Frequently Asked Questions

Regex how to allow spaces?

To allow spaces in your regular expression, you have two main options. The simplest way is to just type a literal space character in your pattern (e.g., hello world). However, the most robust way to match a regex space character or any regex blank character is to use the metacharacter \s. This will match standard spaces, tabs, and line breaks.

What is the regex for new line?

The standard regex for new line is \n. If you are working with Windows-formatted text, a new line might be represented by a carriage return followed by a line feed, which is written as \r\n. To match the end of a line securely (the line end regex), you should use the $ anchor.

What is the meaning of $ in regex?

The meaning of $ in regex is the "End of Line" anchor. It asserts that the pattern must appear at the very end of the string (or the end of a line, if multiline mode is enabled). Conversely, the regex beginning of line is represented by the ^ symbol.

How to use a regex optional character?

To make a character optional, place a question mark in regex (?) immediately after it. For example, the pattern colou?r will successfully match both "color" and "colour", making the "u" a regex optional character.