VOOZH about

URL: https://regexone.com/lesson/whitespaces

⇱ RegexOne - Learn Regular Expressions - Lesson 9: All this whitespace


👁 RegexOne icon
RegexOne
Learn Regular Expressions with simple, interactive exercises.
👁 Interactive Tutorial
Interactive Tutorial 👁 References & More
References & More
All Lessons
Lesson 1: An Introduction, and the ABCs Lesson 1½: The 123s Lesson 2: The Dot Lesson 3: Matching specific characters Lesson 4: Excluding specific characters Lesson 5: Character ranges Lesson 6: Catching some zzz's Lesson 7: Mr. Kleene, Mr. Kleene Lesson 8: Characters optional Lesson 9: All this whitespace Lesson 10: Starting and ending Lesson 11: Match groups Lesson 12: Nested groups Lesson 13: More group work Lesson 14: It's all conditional Lesson 15: Other special characters Lesson X: Infinity and beyond!
Practice Problems
Problem 1: Matching a decimal numbers Problem 2: Matching phone numbers Problem 3: Matching emails Problem 4: Matching HTML Problem 5: Matching specific filenames Problem 6: Trimming whitespace from start and end of line Problem 7: Extracting information from a log file Problem 8: Parsing and extracting data from a URL Problem X: Infinity and beyond!
Language Guides
C# Javascript Java PHP Python
Lesson 9: All this whitespace

When dealing with real-world input, such as log files and even user input, it's difficult not to encounter whitespace. We use it to format pieces of information to make it easier to read and scan visually, and a single space can put a wrench into the simplest regular expression.

The most common forms of whitespace you will use with regular expressions are the space (), the tab (\t), the new line (\n) and the carriage return (\r) (useful in Windows environments), and these special characters match each of their respective whitespaces. In addition, a whitespace special character \s will match any of the specific whitespaces above and is extremely useful when dealing with raw input text.

In the strings below, you'll find that the content of each line is indented by some whitespace from the index of the line (the number is a part of the text to match). Try writing a pattern that can match each line containing whitespace characters between the number and the content. Notice that the whitespace characters are just like any other character and the special metacharacters like the star and the plus can be used as well.

Exercise 9: Matching whitespaces
Task Text
match 1. abc 👁 To be completed
match 2. abc 👁 To be completed
match 3. abc 👁 To be completed
skip 4.abc 👁 To be completed
Solution

We have to match only the lines that have a space between the list number and 'abc'. We can do that by using the expression \d\.\s+abc to match the number, the actual period (which must be escaped), one or more whitespace characters then the text.

If we had used the Kleene Star instead of the plus, we would also match the fourth line, which we actually want to skip.

Solve the above task to continue on to the next problem, or read the Solution.
Find RegexOne useful? Please consider
Donating ($4) via Paypal to support our site.
Lesson Notes
abc… Letters
123… Digits
\d Any Digit
\D Any Non-digit character
. Any Character
\. Period
[abc] Only a, b, or c
[^abc] Not a, b, nor c
[a-z] Characters a to z
[0-9] Numbers 0 to 9
\w Any Alphanumeric character
\W Any Non-alphanumeric character
{m} m Repetitions
{m,n} m to n Repetitions
* Zero or more repetitions
+ One or more repetitions
? Optional character
\s Any Whitespace
\S Any Non-whitespace character
^…$ Starts and ends
(…) Capture Group
(a(bc)) Capture Sub-group
(.*) Capture all
(abc|def) Matches abc or def
2025 © RegexOne
Email | Twitter
Additional Courses
Interactive SQL Lessons