Want to get confident with number regex?
Start with exercises 01, 02, 06, 16, and 17. They cover digits only, exact length, signed integers, and decimals.
Guide: Regex for NumbersSolve 30 real regex problems: number and text validation, email, dates, JavaScript, capturing groups and lookarounds. Learn common mistakes, reveal explanations and test each pattern in the Regex Playground.
Here you solve 30 specific problems and inspect solutions. The practice roadmap is a separate guide for choosing which topics to train and designing your own tests.
Jump to the regex skill you need. Every exercise includes examples, an explanation and a Playground test.
Start with exercises 01, 02, 06, 16, and 17. They cover digits only, exact length, signed integers, and decimals.
Guide: Regex for NumbersExercises 19, 20, 24, 28, and 29 cover capture, identifiers, named groups, and extraction — patterns that map naturally to JavaScript RegExp work.
Guide: JavaScript RegexBuild full-string patterns with anchors, classes and quantifiers.
Match 12345; reject A123, 12-34, and an empty string.
^\d+$
Anchors require the entire string to contain one or more digits.
Common mistake: Without ^ and $, the regex can match digits inside a longer value such as A123.
Test exercise 01 in the PlaygroundMatch 2026; reject 26 and 20260.
^\d{4}$The {4} quantifier requires exactly four digits.
Common mistake: \d{4} without anchors may find four digits inside a longer sequence.
Test exercise 02 in the PlaygroundMatch RegexArena; reject Regex123 and regex-arena.
^[A-Za-z]+$
This class covers ASCII letters; use Unicode-aware patterns when other alphabets matter.
Common mistake: [A-Za-z] does not automatically support accented letters or every Unicode script.
Test exercise 03 in the PlaygroundMatch DEV-2026 and DEV2026; reject DE-2026.
^DEV-?\d{4}$The ? makes the hyphen optional and \d{4} requires four digits.
Common mistake: The hyphen is optional, but DEV and the four digits are still mandatory.
Test exercise 04 in the PlaygroundMatch regex-arena and regex2026; reject spaces and duplicate hyphens.
^[a-z0-9]+(?:-[a-z0-9]+)*$
Each additional block must be separated by a single hyphen.
Common mistake: Avoid leading and trailing hyphens; the non-capturing group keeps the slug structure.
Test exercise 05 in the PlaygroundMatch 12345 and 12345678; reject shorter or longer values.
^\d{5,8}$The {5,8} quantifier sets minimum and maximum length.
Common mistake: The {5,8} range limits length, not the numeric value.
Test exercise 06 in the PlaygroundMatch ERROR connection lost; reject INFO ERROR.
^ERROR\b
The start anchor fixes the position and closes the word.
Common mistake: Without ^, ERROR could also match in the middle of a line.
Test exercise 07 in the PlaygroundFind one or more spaces, tabs, or line breaks so they can be replaced.
\s+
\s represents whitespace and + captures a sequence.
Common mistake: \s includes tabs and newlines, not just ordinary spaces.
Test exercise 08 in the PlaygroundMatch cat, cats, dog, and dogs.
^(cat|dog)s?$
Alternation selects cat/dog and s? makes the plural optional.
Common mistake: Without grouping, alternation can change the scope of anchors and suffixes.
Test exercise 09 in the PlaygroundMatch #22aa77 and #FFFFFF; reject 22aa77.
^#[0-9A-Fa-f]{6}$The hash is literal and the class requires six hexadecimal digits.
Common mistake: This pattern accepts six-digit hex colors; three-digit shorthand needs another rule.
Test exercise 10 in the PlaygroundValidate practical inputs and separate format checks from business rules.
Match 94105 and 94105-1234; reject 9410.
^\d{5}(?:-\d{4})?$Five digits are required and the ZIP+4 suffix is optional.
Common mistake: A ZIP/CEP mask only validates the format, not whether an address exists.
Test exercise 11 in the PlaygroundThis is the Brazilian CEP mask (five digits, a hyphen, then three digits), not the US ZIP code format.
Match 11/10/2026; reject 2026-10-11.
^\d{2}/\d{2}/\d{4}$This checks visual format, not whether the calendar date exists.
Common mistake: A DD/MM/YYYY shape does not guarantee a real date, such as 31/02/2026.
Test exercise 12 in the PlaygroundMatch (415) 555-1234; reject 415 555-1234.
^\(\d{3}\) \d{3}-\d{4}$Literal parentheses must be escaped.
Common mistake: The pattern checks the displayed phone mask, not whether the number is active.
Test exercise 13 in the PlaygroundThis checks the Brazilian (DD) 99999-9999 example format; national phone rules differ by country.
Match dev@example.com; reject dev@site and @example.com.
^[\w.-]+@[\w.-]+\.[A-Za-z]{2,}$Useful for practice, but not a complete implementation of email standards.
Common mistake: A practical email pattern does not cover the complete email address specification.
Test exercise 14 in the PlaygroundMatch https://regexarena.com/en/; reject ftp://example.com.
^https?://[\w.-]+\.[A-Za-z]{2,}(?:/.*)?$The optional s covers HTTP/HTTPS and the last group makes the path optional.
Common mistake: A valid-looking URL is not necessarily reachable or trustworthy.
Test exercise 15 in the PlaygroundMatch 42, -42, and +42; reject 4.2.
^[+-]?\d+$
The sign is optional and the rest must contain digits only.
Common mistake: An optional sign means + and - are allowed, but not required.
Test exercise 16 in the PlaygroundMatch 42, 42.50, and -42,50.
^[+-]?\d+(?:[.,]\d+)?$
The decimal portion is optional and accepts a dot or comma.
Common mistake: Decide whether dot and comma decimal separators are both valid for your locale.
Test exercise 17 in the PlaygroundMatch lines beginning with INFO, WARN, or ERROR followed by YYYY-MM-DD.
^(INFO|WARN|ERROR)\s+\d{4}-\d{2}-\d{2}Alternation captures the log level and the second part requires the date.
Common mistake: Without an end condition, a log prefix may match while trailing text is ignored.
Test exercise 18 in the PlaygroundIn dev@example.com, capture dev as group 1 and example.com as group 2.
^([\w.-]+)@([\w.-]+\.[A-Za-z]{2,})$Parentheses create capture groups that can be reused or inspected.
Common mistake: Captured groups separate user and domain; a full match alone does not extract them.
Test exercise 19 in the PlaygroundMatch userName, _cache, and $value; reject 2users.
^[A-Za-z_$][\w$]*$
The first character has stricter rules; later characters may include word characters and $.
Common mistake: This simplified identifier pattern does not reject JavaScript reserved words.
Test exercise 20 in the PlaygroundExtract data and check context without consuming unnecessary text.
Capture DEV123 only when it is immediately followed by -OK.
^DEV\d{3}(?=-OK$)The lookahead checks context without consuming -OK.
Common mistake: A lookahead checks -OK without consuming those characters in the match.
Test exercise 21 in the PlaygroundMatch lowercase names except the exact word admin.
^(?!admin$)[a-z]+$
The negative lookahead excludes the reserved value before testing the rest.
Common mistake: The negative assertion excludes exact admin; case sensitivity is a separate choice.
Test exercise 22 in the PlaygroundIn $ 150, capture only 150.
(?<=\$ )\d+$
The lookbehind requires the prefix without including it in the match.
Common mistake: Lookbehind requires the R$ prefix but does not include it in the match.
Test exercise 23 in the PlaygroundCapture DEV-2026 in a named group called code.
(?<code>[A-Z]{3}-\d{4})Named groups make extracted data easier to understand in JavaScript and the visualizer.
Common mistake: Named groups expose extracted values; they do not validate business rules by themselves.
Test exercise 24 in the PlaygroundFind the the or regex regex; reject regex arena.
\b(\w+)\s+\1\b
requires the second word to equal what group 1 captured.
Common mistake: A backreference requires the same captured word to appear again.
Test exercise 25 in the PlaygroundRequire at least 8 characters, one uppercase letter, and one digit, using letters and digits only.
^(?=.*[A-Z])(?=.*\d)[A-Za-z\d]{8,}$Two lookaheads verify requirements before the allowed character set is validated.
Common mistake: A shape check is not a complete password security policy.
Test exercise 26 in the PlaygroundMatch files ending in jpg, jpeg, png, or webp using the i flag.
^.+\.(?:jpe?g|png|webp)$
The non-capturing group collects extensions and jpe?g covers jpg/jpeg.
Common mistake: Use the i flag when .PNG and .JPG should also match; the extension is not file validation.
Test exercise 27 in the PlaygroundCapture day, month, and year from 11/10/2026 using named groups.
^(?<day>\d{2})/(?<month>\d{2})/(?<year>\d{4})$Each date part becomes available by name for later extraction.
Common mistake: Named groups extract day, month and year; date validity still requires another check.
Test exercise 28 in the PlaygroundFor an HTTP/HTTPS URL, capture the host as host and the optional path as path.
^https?://(?<host>[\w.-]+\.[A-Za-z]{2,})(?<path>/.*)?$Named groups split useful parts while still checking the overall format.
Common mistake: Extracting a host and path does not prove a URL can actually be reached.
Test exercise 29 in the PlaygroundCapture DEV-2026 only after ID: and before ;OK.
(?<=ID:)(?<code>[A-Z]{3}-\d{4})(?=;OK$)Combine lookbehind, a named group, and lookahead in one challenge.
Common mistake: Lookbehind and lookahead check surrounding text without adding it to the match.
Test exercise 30 in the PlaygroundTurn practice into a habit: test patterns, play missions, and use smart review to reinforce weaker concepts.
Practice with eight documented mistakes and four realistic developer workflows. Compare the broken pattern, the fix, and negative test cases before you reuse a solution.
Open the debugging lab