Reference

20 Regex Examples to Copy, Test, and Adapt

Examples are most useful when you understand the requirement behind them. Treat these patterns as starting points, add negative test cases, and adapt them to your environment.

Before you start: test every pattern with examples that should match and examples that should fail.

Basic building blocks

These small patterns appear inside many larger solutions.

\d+          # one or more digits\n\w+          # one or more word chars\n^abc$        # exactly abc\n[A-Z]{2}     # two uppercase letters

Format validation

Anchors are common when the entire input must follow one structure.

^\d{5}-\d{3}$                  # formatted Brazilian ZIP/CEP\n^\d{2}/\d{2}/\d{4}$            # structural date\n^\(\d{2}\) \d{5}-\d{4}$       # formatted phone

Web and development examples

URLs, file extensions, and logs are good exercises for groups and alternation.

\.(jpg|png|webp)$               # image extension\n^(INFO|WARN|ERROR)\s+            # log level\nhttps?://                         # http/https prefix

How to adapt examples safely

Never copy a pattern without writing values that should match and values that should fail. Small requirement changes can completely change the right Regex.

Next step: test it for real

Use the Playground to experiment with the pattern, then practice in the game with feedback on false positives and false negatives.

Practice in the gameTest in Playground

Keep learning

← Back to learning hub