TExcelTAD
Home › Regular Expressions › Regex Exercises

Regex Exercises

Time to practise! Each exercise has a starter tester whose pattern is deliberately incomplete — it runs, but matches too much or too little. Edit the first line until the matches are exactly the ones described under "Expected", then compare with the solution.

Tip: Change one thing at a time and press Ctrl+Enter after each change. Watching how the match list reacts is the fastest way to learn.

# Exercise Topics
1 Years 1900–2099 quantifiers, alternation, word boundaries
2 Hex colours classes, counted repetition
3 Hashtags lookbehind
4 Usernames anchors, multiline
5 Doubled words backreferences
6 Prices groups, optional parts
7 Lines without DEBUG negative lookahead
8 Query-string pairs named groups, negated classes

1. Years 1900–2099

Match four-digit years from 1900 to 2099, but not parts of longer numbers or other numbers.

Expected: 1987, 2004, 2023 (3 matches).

regex · try it yourself
/\d+/g
Born 1987, moved 2004, room 12, code 31415, year 2023, not 1850
6 matches
#1 "1987" at index 5
#2 "2004" at index 17
#3 "12" at index 28
#4 "31415" at index 37
#5 "2023" at index 49
#6 "1850" at index 59
Show solution
regex
/\b(?:19|20)\d{2}\b/g

(?:19|20) fixes the century, \d{2} adds two more digits, and the word boundaries stop 31415 from contributing a match.

2. Hex colours

Match CSS hex colours with exactly 3 or 6 hex digits.

Expected: #fff, #00AAff, #123 (3 matches).

regex · try it yourself
/#\w+/g
#fff #00AAff #ggg #1234 #123 #abcdefg
6 matches
#1 "#fff" at index 0
#2 "#00AAff" at index 5
#3 "#ggg" at index 13
#4 "#1234" at index 18
#5 "#123" at index 24
#6 "#abcdefg" at index 29
Show solution
regex
/#(?:[0-9a-fA-F]{3}){1,2}\b/g

Three hex digits, once or twice. The final \b rejects #1234 and #abcdefg, where more word characters follow.

3. Hashtags

Match hashtags (# followed by letters, digits or _), but not when the # is glued to a word, as in issue#42.

Expected: #regex, #JS_tips, #2025 (3 matches).

regex · try it yourself
/#\w/g
Loving #regex and #JS_tips! See issue#42. #2025
4 matches
#1 "#r" at index 7
#2 "#J" at index 18
#3 "#4" at index 37
#4 "#2" at index 42
Show solution
regex
/(?<!\w)#\w+/g

\w+ takes the whole tag, and the negative lookbehind refuses a # that comes right after a word character.

4. Usernames

Each line is a username. A valid one is 3 to 16 characters long, uses only lowercase letters, digits and _, and starts with a letter. Match only the valid lines.

Expected: linh_99, bao, an_nguyen_2025 (3 matches).

regex · try it yourself
/^[a-z]+/gm
linh_99
bao
x1
9lives
an_nguyen_2025
Mai
this_name_is_far_too_long
5 matches
#1 "linh" at index 0
#2 "bao" at index 8
#3 "x" at index 12
#4 "an" at index 22
#5 "this" at index 41
Show solution
regex
/^[a-z][a-z0-9_]{2,15}$/gm

One letter, then 2–15 more allowed characters (3–16 in total). Both anchors plus the m flag make each whole line pass or fail.

5. Doubled words

Find accidentally repeated words, ignoring case.

Expected: the the, is Is (2 matches), with the repeated word in group 1.

regex · try it yourself
/\b(\w+)\s+\w+\b/gi
I saw the the cat. It is Is it not? Very good.
5 matches
#1 "I saw" at index 0
   group 1: "I"
#2 "the the" at index 6
   group 1: "the"
#3 "It is" at index 19
   group 1: "It"
#4 "Is it" at index 25
   group 1: "Is"
#5 "Very good" at index 36
   group 1: "Very"
Show solution
regex
/\b(\w+)\s+\1\b/gi

The backreference \1 requires the second word to repeat the first; the i flag makes the comparison case-insensitive.

6. Prices

Capture dollar amounts without the $ sign in group 1. Amounts may have thousands separators and an optional two-digit cents part.

Expected groups: 1,299.99, 15, 3.50 (3 matches).

regex · try it yourself
/\$\d+/g
Total $1,299.99, shipping $15, tax $3.50, and 20 dollars.
3 matches
#1 "$1" at index 6
#2 "$15" at index 26
#3 "$3" at index 35
Show solution
regex
/\$(\d{1,3}(?:,\d{3})*(?:\.\d{2})?)/g

One to three digits, any number of ,ddd blocks, then optional cents — all inside group 1. Non-capturing groups keep the numbering simple.

7. Lines without DEBUG

Match every complete line that does not contain the word DEBUG.

Expected: the three INFO/WARN/ERROR lines.

regex · try it yourself
/^.*$/gm
INFO server started
DEBUG config loaded
WARN disk 91% full
DEBUG cache warm
ERROR request failed
5 matches
#1 "INFO server started" at index 0
#2 "DEBUG config loaded" at index 20
#3 "WARN disk 91% full" at index 40
#4 "DEBUG cache warm" at index 59
#5 "ERROR request failed" at index 76
Show solution
regex
/^(?!.*\bDEBUG\b).*$/gm

At the start of each line, the negative lookahead scans ahead; if DEBUG appears anywhere in the line, the whole line is rejected.

8. Query-string pairs

Extract each key=value pair from a URL query string, with named groups key and value. Values may be empty.

Expected: page/2, sort/price_desc, q/red+shoes, debug/empty (4 matches).

regex · try it yourself
/(\w+)=/g
/shop?page=2&sort=price_desc&q=red+shoes&debug=
4 matches
#1 "page=" at index 6
   group 1: "page"
#2 "sort=" at index 13
   group 1: "sort"
#3 "q=" at index 29
   group 1: "q"
#4 "debug=" at index 41
   group 1: "debug"
Show solution
regex
/[?&](?<key>\w+)=(?<value>[^&]*)/g

Each pair starts after ? or &. The value is "anything up to the next &", and * allows it to be empty.

Test yourself

Which pattern matches a whole line only if it is 3–5 digits?

  1. /\d{3,5}/gm
  2. /^\d{3,5}$/gm
  3. /^\d+$/gm
  4. /\b\d{3,5}/gm

In exercise 3, why use (?<!\w) instead of \b before #?

  1. \b is not supported
  2. # is not a word character, so \b# requires a word character BEFORE it — the opposite of what we want
  3. Lookbehind is faster
  4. \b ignores digits