iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Literals & Metachars

Regex literals in code: how they differ across JavaScript, Python, Java, Go, Ruby, PHP, and shell. Same patterns, slightly different surface syntax.

Regex — literals across languages

EXAMPLE
// ===== JavaScript =====
const re = /^\d{4}-\d{2}-\d{2}$/;
const flags = 'gim';
const re2 = new RegExp('^\\d{4}', 'g');
'2024-04-10'.match(re);

// ===== Python =====
// Use raw strings (r'...') to skip Python escape rules
import re
pattern = r'^\d{4}-\d{2}-\d{2}$'
re.match(pattern, '2024-04-10')
re.findall(r'\b\w+\b', 'hello world')

// ===== Go =====
import "regexp"
re := regexp.MustCompile(\`^\d{4}-\d{2}-\d{2}$\`)
re.MatchString("2024-04-10")
re.FindAllString("123 abc 456", -1)

// Note: Go uses RE2 — no backreferences, no lookarounds; linear time.

// ===== Ruby =====
re = /^\d{4}-\d{2}-\d{2}$/
'2024-04-10'.match(re)
'hello world'.scan(/\b\w+\b/)

// ===== Java =====
import java.util.regex.*;
Pattern p = Pattern.compile("^\\d{4}-\\d{2}-\\d{2}$");
Matcher m = p.matcher("2024-04-10");
if (m.matches()) { System.out.println("hit"); }

// Watch the double-escaping inside a Java string literal.

// ===== C# =====
using System.Text.RegularExpressions;
var re = new Regex(@"^\d{4}-\d{2}-\d{2}$");
re.IsMatch("2024-04-10");

// Verbatim @"..." strings avoid escaping backslashes.

// ===== PHP =====
preg_match('/^\\d{4}-\\d{2}-\\d{2}$/', '2024-04-10', $m);
preg_match_all('/\\b\\w+\\b/', 'hello world', $m);

// PHP uses Perl-compatible regex (PCRE); flags after the closing delimiter:
preg_match('/abc/i', 'ABC');

// ===== Bash (POSIX) =====
[[ '2024-04-10' =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]] && echo 'ok'
# \d doesn't work in BRE/ERE; use [0-9]

# grep, sed (GNU):
grep -E '^[0-9]{4}-[0-9]{2}-[0-9]{2}$' file.txt
grep -P '^\d{4}-\d{2}-\d{2}$' file.txt    # -P enables PCRE on GNU grep

// ===== Common gotchas across languages =====
// - Anchors: ^ $ behave differently with multi-line flag (m)
// - Word boundary: \b matches differently around Unicode (use /u in JS, \b{w} in others)
// - Case-insensitive: i flag everywhere; behaviour varies on locales
// - Backreferences not supported in RE2 (Go, RE2 in C++)
// - Lookbehind not in RE2; uneven support in older engines

// ===== Patterns to internalise =====
// - Raw strings in Python (r'...') and verbatim in C# (@"...")
// - Linear-time engines (RE2) for any user-supplied pattern (DoS-safe)
// - Compile once, reuse the compiled object in hot loops
// - Test patterns with positives, negatives, and a fuzzy near-miss

// ===== Pitfalls =====
// - Forgetting raw strings -> backslashes turn into other characters
// - Catastrophic backtracking from user input (^(a+)+$)
// - Mixing POSIX BRE/ERE vs PCRE features across shell tools
// - Editor-friendly regex (extended /x mode) shipped without comments

Why it matters

The patterns travel; the surface syntax does not. Raw strings in Python, verbatim in C#, double-escape in Java, compile-once everywhere. For user-supplied patterns reach for a linear-time engine (RE2) — backtracking + adversarial input has paged enough teams already.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
// Most characters match themselves.
// Metacharacters need escaping: . ^ $ * + ? ( ) [ ] { } | \ /
Try it Yourself »

Exercise

Metacharacter that matches any single character.

/c t/

Discussion

Loading…