TExcelTAD
Home › Regular Expressions › Regex in Other Languages

Regex in Other Languages

The core syntax — classes, quantifiers, groups, anchors — is almost identical everywhere. What changes from language to language is the API (how you call it), the string escaping you need around the pattern, and a handful of advanced features. This lesson shows the same task in Java, C#, Go and Rust so you can recognise the patterns. (These snippets are for reading; the playgrounds in this course run JavaScript and Python.)

The task: find every key=value pair in "cpu=73 mem=41 disk=88" and print them.

Java — java.util.regex

java
import java.util.regex.*;

class Main {
    public static void main(String[] args) {
        Pattern p = Pattern.compile("(?<key>\\w+)=(?<val>\\d+)");
        Matcher m = p.matcher("cpu=73 mem=41 disk=88");
        while (m.find()) {
            System.out.println(m.group("key") + " -> " + m.group("val"));
        }
        System.out.println("2025-07-31".replaceAll("(\\d+)-(\\d+)-(\\d+)", "$3/$2/$1"));
        System.out.println("abc123".matches("[a-z]+\\d+"));   // whole string must match
    }
}
  • Patterns are plain strings, so every backslash is doubled. Java 15+ text blocks don't change that.
  • matches() requires the entire string to match; find() searches.
  • Supports lookbehind (bounded width), atomic groups (?>…), possessive quantifiers a++, inline flags (?i), \p{L}.

C# — System.Text.RegularExpressions

csharp
using System;
using System.Text.RegularExpressions;

var re = new Regex(@"(?<key>\w+)=(?<val>\d+)");
foreach (Match m in re.Matches("cpu=73 mem=41 disk=88"))
{
    Console.WriteLine(
quot;{m.Groups["key"].Value} -> {m.Groups["val"].Value}"); } Console.WriteLine(Regex.Replace("2025-07-31", @"(\d+)-(\d+)-(\d+)", "$3/$2/$1")); Console.WriteLine(Regex.IsMatch("abc123", @"^[a-z]+\d+
quot;));
  • Verbatim strings @"..." avoid double escaping.
  • .NET has some of the richest features: variable-length lookbehind, balancing groups, RegexOptions.RightToLeft, match timeouts, and source-generated regexes ([GeneratedRegex]) for speed.
  • RegexOptions.NonBacktracking (since .NET 7) switches to a linear-time engine.

Go — regexp

go
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`(?P<key>\w+)=(?P<val>\d+)`)
	for _, m := range re.FindAllStringSubmatch("cpu=73 mem=41 disk=88", -1) {
		fmt.Println(m[1], "->", m[2])
	}
	date := regexp.MustCompile(`(\d+)-(\d+)-(\d+)`)
	fmt.Println(date.ReplaceAllString("2025-07-31", "${3}/${2}/${1}"))
	fmt.Println(regexp.MustCompile(`^[a-z]+\d+

  
    
    
    Regex in Other Languages - Regular Expressions · ExcelTAD
    
    
    
    
    
    
    
    
    
    
    
    
    
    
  
  
    ).MatchString("abc123"))
}
  • Backtick raw strings make patterns readable.
  • Uses the RE2 design: guaranteed linear time, so no backreferences and no lookarounds.
  • The -1 argument means "all matches"; pass n to limit.

Rust — the regex crate

rust
use regex::Regex;

fn main() {
    let re = Regex::new(r"(?<key>\w+)=(?<val>\d+)").unwrap();
    for caps in re.captures_iter("cpu=73 mem=41 disk=88") {
        println!("{} -> {}", &caps["key"], &caps["val"]);
    }
    let date = Regex::new(r"(\d+)-(\d+)-(\d+)").unwrap();
    println!("{}", date.replace_all("2025-07-31", "$3/$2/$1"));
    println!("{}", Regex::new(r"^[a-z]+\d+
quot;).unwrap().is_match("abc123")); }
  • regex is an external crate (not in std), maintained by the Rust project.
  • Also linear-time: no backreferences, no lookarounds. The fancy-regex crate adds them back on top of a backtracking engine.
  • \w, \d and \b are Unicode-aware by default.

All four print the same

cpu -> 73
mem -> 41
disk -> 88
31/07/2025
true

Here is the same logic in JavaScript, which you can run:

js · try it yourself
const re = /(?<key>\w+)=(?<val>\d+)/g;
for (const m of "cpu=73 mem=41 disk=88".matchAll(re)) {
  console.log(`${m.groups.key} -> ${m.groups.val}`);
}
console.log("2025-07-31".replace(/(\d+)-(\d+)-(\d+)/, "$3/$2/$1"));
console.log(/^[a-z]+\d+$/.test("abc123"));
cpu -> 73
mem -> 41
disk -> 88
31/07/2025
true

Side-by-side comparison

Java C# Go Rust JavaScript
Pattern string "\\d" @"\d" `\d` r"\d" /\d/
Find all while (m.find()) Matches() FindAll…(s, -1) find_iter / captures_iter matchAll
Whole-string test matches() ^…$ + IsMatch ^…$ + MatchString ^…$ + is_match ^…$ + test
Replace ref $1 ${n} $1 ${n} ${1} ${n} $1 ${n} $1
lt;n>
Lookaround yes yes no no yes
Backreferences yes yes no no yes
Linear-time guarantee no optional yes yes no
Unicode \w by default no yes no yes no

Tip: On the command line, grep -E uses POSIX extended syntax, grep -P (GNU) uses Perl-compatible syntax, and ripgrep uses Rust's regex — so \d and lookarounds behave differently in each.

Test yourself

Why are backslashes doubled in Java patterns like "\\d+"?

  1. Java's regex syntax requires it
  2. The Java string literal consumes one level of escaping
  3. To make the pattern greedy
  4. For Unicode support

Which of these engines does NOT support lookarounds?

  1. Java
  2. .NET
  3. Go regexp
  4. JavaScript

In Java, what does "abc123x".matches("[a-z]+\\d+") return?

  1. true
  2. false
  3. "abc123"
  4. It throws