ToolsleFree tools · toolsle.com

Regular expression

//

Test string

Enter a pattern to begin

Regex Tester

Regex Tester — Test and Debug Regular Expressions Online

Our free online regex tester lets you write, test, and debug regular expressions in real time. Matches are highlighted inline as you type, capture groups are shown in distinct colors, and a built-in cheat sheet and common pattern library help you build patterns faster. Switch to Replace mode to test substitutions with backreferences.

Regular Expression Syntax Reference

Character classes and metacharacters

PatternMatchesExample
.Any character except newlinec.t matches "cat", "cut", "c3t"
\dDigit 0-9\d\d matches "42", "99"
\DNon-digit\D+ matches "hello", "abc"
\wWord char [a-zA-Z0-9_]\w+ matches "hello_world"
\WNon-word character\W matches space, punctuation
\sWhitespace (space, tab, newline)\s+ matches spaces between words
\SNon-whitespace\S+ matches any non-space token
[abc]Any of: a, b, c[aeiou] matches any vowel
[^abc]Not: a, b, c[^0-9] matches non-digits
[a-z]Range: a through z[a-zA-Z] matches any letter
^Start of string/line^Hello matches "Hello world"
$End of string/lineworld$ matches "Hello world"
\bWord boundary\bcat\b matches "cat" not "catch"

Quantifiers

QuantifierMeaningGreedy?Example
*0 or moreYesab* matches "a", "ab", "abb"
+1 or moreYesab+ matches "ab", "abb" not "a"
?0 or 1Yesab? matches "a" or "ab"
{n}Exactly n\d{4} matches "2024"
{n,}n or moreYes\d{2,} matches "42", "100"
{n,m}Between n-mYes\d{2,4} matches "42", "100"
*?0 or moreNo (lazy)a.*?b matches shortest possible
+?1 or moreNo (lazy)a.+?b matches shortest possible

Groups and lookarounds

SyntaxTypeDescription
(abc)Capture groupCaptures and remembers matched text
(?:abc)Non-capturingGroups without capturing
(?<name>abc)Named groupCapture with a label
(?=abc)LookaheadMatches if followed by abc
(?!abc)Neg lookaheadMatches if NOT followed by abc
(?<=abc)LookbehindMatches if preceded by abc
(?<!abc)Neg lookbehindMatches if NOT preceded by abc

Regex flags

FlagNameEffectExample
gGlobalFind all matches, not just first/\d+/g finds all numbers
iCase InsensitiveMatch regardless of case/hello/i matches "Hello", "HELLO"
mMultiline^ and $ match line start/end/^\w+/m matches first word of each line
sDotallDot matches newline too/.+/s matches across lines
uUnicodeFull Unicode matching/\p{L}+/u matches Unicode letters
yStickyMatch only from lastIndexUsed for parsing streams

Common regex patterns

Use casePatternNotes
Email validation^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$Basic — full RFC 5322 is much more complex
US Phone number(\+1[-.\s]?)?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}Handles various formats
URLhttps?:\/\/[\w-]+(\.[\w-]+)+(\/[\w-./?%&=]*)?Simplified URL match
HEX color#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})3 or 6 digit hex
IP address\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\bSimplified — use full pattern for validation
Date YYYY-MM-DD\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])With basic month/day validation
ZIP code (US)^\d{5}(-\d{4})?$5 digit or ZIP+4

Greedy vs lazy matching — key difference

One of the most important concepts in regex is the difference between greedy and lazy quantifiers. This determines how much text a quantifier tries to consume:

Consider the string: <b>Hello</b> and <b>World</b>

Greedy: <b>.*</b> matches the entire string from the first <b> to the last </b> — consuming everything in between.

Lazy: <b>.*?</b> matches <b>Hello</b> and <b>World</b> separately — stopping at the first possible </b>.

In most HTML parsing and text extraction scenarios, lazy quantifiers give the intended result.

Regex in different programming languages

LanguageRegex syntaxTest methodMatch method
JavaScript/pattern/flags or new RegExp()regex.test(str)str.match(regex)
Pythonre module, r"pattern"re.match() or re.search()re.findall()
JavaPattern.compile("pattern")matcher.matches()matcher.find()
PHPpreg_match("/pattern/", $str)preg_match()preg_match_all()
C#Regex.IsMatch("pattern")Regex.IsMatch()Regex.Matches()
Ruby/pattern/ or Regexp.newstr =~ /pattern/str.scan(/pattern/)
Goregexp.MustCompile("pattern")regexp.MatchString()re.FindAllString()

Frequently asked questions

How do I match a literal dot in regex?

In regex, a dot (.) is a metacharacter that matches any character except a newline. To match a literal dot, escape it with a backslash: . For example, to match "example.com" use example\.com rather than example.com(which would also match "exampleXcom").

How do I make a regex case insensitive?

Add the "i" flag to your regex. In JavaScript: /hello/imatches "Hello", "HELLO", "hElLo" and any other case combination. Toggle the "i" flag chip in this tester to enable case-insensitive matching.

What does \b mean in regex?

\b is a word boundary assertion. It matches the position between a word character (\w) and a non-word character (\W). For example, \bcat\bmatches the word "cat" in "the cat sat" but NOT in "catch" or "concatenate", because in those words "cat" is not surrounded by word boundaries.

How do I match multiple lines in regex?

Use the "m" (multiline) flag to make ^ and $ match the start and end of each line rather than just the start and end of the entire string. Use the "s" (dotall) flag to make the dot (.) also match newline characters, allowing patterns to span multiple lines.

How do I extract text from between tags using regex?

Use a capture group with a lazy quantifier: <tag>(.*?)</tag> captures the content between the tags. The lazy *? ensures the match stops at the first closing tag rather than consuming everything to the last closing tag. For production HTML parsing, always use a proper HTML parser rather than regex.