Orbitautil
Implementation Guide

Regex Code Snippets

You have built and tested your regular expression. Now see how to run it, list matches and read capture groups in JavaScript, Python and PHP. The three snippets below use the same simple email extractor and print the same two matches. It is not a validator: it reads at most 4 letters after the last dot, so [email protected] comes out as [email protected].

JavaScript

JavaScript has built-in support for regular expressions using the RegExp object or literal syntax /.../.

const text = "Contact us at [email protected] or [email protected].";
const pattern = /([a-zA-Z0-9._-]+)@([a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})/g;

// 1. Check if it matches
const isValid = pattern.test(text);
console.log("Has email?", isValid);

// 2. Get all matches
const matches = text.match(pattern);
console.log("Matches:", matches);

// 3. Extract capture groups
let match;
// Reset lastIndex since we are reusing a global pattern
pattern.lastIndex = 0;
while ((match = pattern.exec(text)) !== null) {
  console.log("Found: " + match[0]);
  console.log("User: " + match[1] + ", Domain: " + match[2]);
}

Python

Python uses the built-in re module. Use raw strings r"..." to avoid escaping backslashes.

import re

text = "Contact us at [email protected] or [email protected]."
pattern = r"([a-zA-Z0-9._-]+)@([a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})"

# 1. Check if it matches (search finds the first match anywhere)
if re.search(pattern, text):
    print("Has email: True")

# 2. Get all matches (returns list of tuples if there are capture groups)
matches = re.findall(pattern, text)
print("Matches:", matches)

# 3. Extract capture groups
for match in re.finditer(pattern, text):
    print(f"Found: {match.group(0)}")
    print(f"User: {match.group(1)}, Domain: {match.group(2)}")

PHP

PHP uses PCRE (Perl Compatible Regular Expressions) functions like preg_match and preg_match_all.

<?php
$text = "Contact us at [email protected] or [email protected].";
$pattern = "/([a-zA-Z0-9._-]+)@([a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4})/";

// 1. Check if it matches
if (preg_match($pattern, $text)) {
    echo "Has email: True\n";
}

// 2. Get all matches and capture groups
// PREG_SET_ORDER groups results by each match
if (preg_match_all($pattern, $text, $matches, PREG_SET_ORDER)) {
    foreach ($matches as $match) {
        echo "Found: " . $match[0] . "\n";
        echo "User: " . $match[1] . ", Domain: " . $match[2] . "\n";
    }
}
?>

Frequently asked questions

Do all programming languages use the exact same regex engine?

No. While the syntax is very similar, different languages use different regex engines (e.g., PCRE in PHP, Python's re module, JavaScript's RegExp). There are slight differences in advanced features like lookbehinds and named capture groups.

How do I handle backslashes in strings?

In many languages like Python and Java, you must escape backslashes in regular string literals (e.g., `"\\d"`), or use raw strings (like Python's `r"\d"`) to avoid double-escaping.