A
cd ..
Tools

Regex Testing & Patterns

Test and debug regular expressions with common patterns and tools.

2025-11-05
regex, patterns, text

Test regex with grep

echo "test123" | grep -P "test\d+"

Basic patterns

.       # Any character
\d      # Digit [0-9]
\D      # Not digit
\w      # Word character [a-zA-Z0-9_]
\W      # Not word character
\s      # Whitespace
\S      # Not whitespace

Anchors

^       # Start of line
$       # End of line
\b      # Word boundary
\B      # Not word boundary

Quantifiers

*       # 0 or more
+       # 1 or more
?       # 0 or 1
{3}     # Exactly 3
{3,}    # 3 or more
{3,5}   # 3 to 5
*?      # Lazy (non-greedy)

Character classes

[abc]      # a, b, or c
[^abc]     # Not a, b, or c
[a-z]      # Lowercase letters
[A-Z]      # Uppercase letters
[0-9]      # Digits
[a-zA-Z0-9]# Alphanumeric

Groups and capture

(abc)      # Capture group
(?:abc)    # Non-capturing group
(a|b|c)    # Alternation
\1         # Backreference to group 1

Common patterns

Email

[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}

URL

https?://[^\s]+

IPv4 address

\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b

Phone number (US)

\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}

Date (YYYY-MM-DD)

\d{4}-\d{2}-\d{2}

Time (HH:MM)

\d{2}:\d{2}

Credit card

\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}

Hex color

#[0-9A-Fa-f]{6}

Username

^[a-zA-Z0-9_]{3,16}$

Password (strong)

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$

Test with sed

echo "hello123world" | sed -n '/[a-z]*[0-9]*[a-z]*/p'

Test with awk

echo "test@example.com" | awk '/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/ {print "Valid email"}'

Test with Perl

echo "test123" | perl -ne 'print if /test\d+/'

Python regex

import re

pattern = r'\d+'
text = "abc123def456"
matches = re.findall(pattern, text)
print(matches)  # ['123', '456']

JavaScript regex

const pattern = /\d+/g;
const text = "abc123def456";
const matches = text.match(pattern);
console.log(matches);  // ['123', '456']

Online regex testers

  • regex101.com
  • regexr.com
  • regextester.com

Common use cases

Extract emails from text

grep -oP '\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b' file.txt

Extract URLs

grep -oP 'https?://[^\s]+' file.txt

Extract IP addresses

grep -o -E '\b([0-9]{1,3}\.){3}[0-9]{1,3}\b' file.txt

Find phone numbers

grep -P '\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}' file.txt

Validate input

read -p "Enter email: " email
if [[ $email =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then
    echo "Valid email"
else
    echo "Invalid email"
fi

Lookahead/Lookbehind

(?=...)    # Positive lookahead
(?!...)    # Negative lookahead
(?<=...)   # Positive lookbehind
(?<!...)   # Negative lookbehind

Example: Password must contain letter and digit

^(?=.*[a-z])(?=.*\d).+$

Flags

g    # Global (all matches)
i    # Case insensitive
m    # Multiline
s    # Dot matches newline
x    # Ignore whitespace

Escape special characters

. \ + * ? [ ^ ] $ ( ) { } = ! < > | : -

Common mistakes

# Wrong: .* is greedy
echo "<p>test</p>" | grep -oP '<.*>'
# Output: <p>test</p>

# Right: .*? is lazy
echo "<p>test</p>" | grep -oP '<.*?>'
# Output: <p>, </p>

Debug regex

# Use verbose mode (Perl)
perl -e '
$text = "test123";
$text =~ /
    test   # Match "test"
    \d+    # Then digits
/x and print "Match\n";
'

Replace with sed

# Replace emails
sed 's/[a-zA-Z0-9._%+-]\+@[a-zA-Z0-9.-]\+\.[a-zA-Z]\{2,\}/[EMAIL]/g' file.txt

# Replace phone numbers
sed 's/\([0-9]\{3\}\)[-. ]\?\([0-9]\{3\}\)[-. ]\?\([0-9]\{4\}\)/(\1) \2-\3/g' file.txt

Bash regex

text="My IP is 192.168.1.100"
if [[ $text =~ ([0-9]+\.){3}[0-9]+ ]]; then
    echo "IP found: ${BASH_REMATCH[0]}"
fi

Test script

#!/bin/bash

test_regex() {
    local pattern=$1
    local text=$2
    
    if echo "$text" | grep -qP "$pattern"; then
        echo "✓ MATCH: '$text'"
    else
        echo "✗ NO MATCH: '$text'"
    fi
}

# Test email pattern
EMAIL_PATTERN='[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}'

test_regex "$EMAIL_PATTERN" "test@example.com"
test_regex "$EMAIL_PATTERN" "invalid.email"

Was this useful?

Share with your team

Browse More