Regular expressions (RegEx) are an essential tool for developers and programmers. They allow you to define search patterns to match text strings and extract specific data from them.
With the rise of web applications and the need to validate user inputs, RegEx has become ubiquitous across programming languages. That‘s why having a good RegEx tester to build, test and troubleshoot your expressions is critical.
In this comprehensive guide, we will cover:
- What are RegEx and why they are useful
- RegEx syntax basics
- Commonly used RegEx patterns
- 10 handy online RegEx testers
- RegEx learning resources
What are Regular Expressions?
Regular expressions, also known as "RegEx" or "Regex", are sequence of characters that define a search pattern. For example, the RegEx /w+b/
will match all words ending with "b".
RegEx can be used to:
- Validate forms: email addresses, phone numbers, postal codes, etc.
- Extract specific text patterns from strings
- Find and replace text
- Split text based on a delimiter
Virtually all programming languages have built-in support for RegEx. That‘s why they are so popular among developers.
RegEx Syntax Basics
While a full RegEx syntax guide is beyond this article‘s scope, let‘s review some common constructs:
Anchors
Anchors allow you to match positions before, after or between characters:
^
– Start of string/line$
– End of string/line\b
– Word boundary
Quantifiers
Quantifiers specify how many instances of the previous character/group you want to match:
*
– Zero or more+
– One or more?
– Zero or one{3}
– Exactly 3{3,5}
– Between 3 and 5
Character Classes
Character classes allow you to match different types of characters:
\d
– Any digit\w
– Alphanumeric character.
– Any character except newline[abc]
– a, b or c[^abc]
– Not a, b or c
Grouping
Grouping constructs allow you to combine expressions:
(expr)
– Capture group(?:expr)
– Non-capturing group(expr1|expr2)
– Match expr1 or expr2
Top 10 RegEx Testers
When building RegEx patterns, having a good tester to try out and troubleshoot your expressions is indispensable.
Here are 10 great online RegEx testers:
1. Regex101
Regex101 is one of the most popular RegEx testers out there. It supports JavaScript, Python, PHP and Go languages.
Features:
- Live testing as you type
- Detailed highlighting and explanations
- Search for patterns in test strings
- Save expressions for later
- RegEx references and cheatsheets
2. RegExr
RegExr is another top-notch tester focused on JavaScript RegEx.
Features:
- Real-time results updating
- Roll over matches for details
- Expression validation via tests
- Share saved expressions
- Full RegEx documentation
3. Pythex
Pythex is specialized for Python RegEx. It comes with Pyton-specific flags:
- Ignore case
- Multiline
- Dotall
- Verbose
Easy to use interface where you can write a Python expression on one side and input test cases on the other.
4. Rubular
Rubular assists with building Ruby RegEx and testing them. It uses the Ruby 2.5+ engine to evaluate expressions.
Test your patterns against test cases, use substitution to replace text and leverage different Ruby RegEx flags.
5. PHP Live Regex
PHP Live Regex is a RegEx tester tailored for PHP.
It shows you matches, substitutions and can validate your expressions via unit testing. There are also nice code generation utilities to integrate your RegEx into PHP apps quickly.
6. Regex Crossword
If you like puzzles, check out Regex Crossword. It has a wide selection of RegEx-themed puzzles, ranging from beginner to very advanced.
It‘s a more engaging way to become fluent in RegEx syntax than dry documentation. Available as a mobile app too.
7. Debuggex
Debuggex provides a minimalistic RegEx testing interface for JavaScript, Python and Perl.
Notable features:
- Share your expressions via unique URL
- Embed expressions into StackOverflow
- Regex references and cheat sheets
8. Free Formatter
FreeFormatter has a simple JavaScript-based tester that is great for quick ad-hoc testing.
It shows the matches in your input text, replacements and supports different flags like case insensitivity, multiline, etc.
9. Extends Class
Extends Class provides a multi-language RegEx tester supporting JavaScript, Python, Ruby, Java, PHP and more.
While the UI is very basic, it allows you to quickly test how a RegEx behaves across different engines.
10. Web Toolkit Online
Web Toolkit Online packs different web developer tools including a helpful RegEx tester.
Features:
- Test substitutions and get match info
- Case sensitivity toggle
- Copy expressions to clipboard
This covers some of the best online RegEx testers available today. They all provide easy ways to evaluate how expressions match test cases and share these for collaboration.
Common RegEx Use Cases
Here are some common examples where using RegEx shines:
Email Validation
Check if an input string has the proper email syntax:
/^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$/
Password Strength Check
Validate if a password is secure enough by enforcing:
- At least one upper and lower case letter
- At least one number
- Minimum 6 characters
/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).{6,}/
Phone Number Validation
Check if a phone number matches common formats like:
/^[(]{0,1}[0-9]{3}[)]{0,1}[-\s\.]{0,1}[0-9]{3}[-\s\.]{0,1}[0-9]{4}$/
Extract All Emails
From a block of text, extract substrings that match email syntax:
/[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+/g
As you can see, RegEx is very versatile for common text processing and validation tasks.
Learn Regular Expressions
Here are some recommendations if you want to level up your RegEx skills:
Coursera Guided Projects
Coursera has short 1-2 hour hands-on RegEx projects for Python and Java that are easy to follow. For example:
Udemy Courses
For more extensive RegEx training, these Udemy courses are very popular:
- Complete RegEx Course for Beginners – RegEx from zero to advanced
- Python RegEx Course with Projects – Applied RegEx skills for Python
Conclusion
I hope this guide gives you a good idea of what RegEx is, basic syntax, use cases and the many online testers available.
Fluency with regular expressions will make you a better programmer. You‘ll be able to easily solve common data processing and validation tasks.
Start testing some expressions using these RegEx testers and bookmark those that best meet your needs.