Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 38 · Regex in Languages and Tools

Regex in Other Languages

See the same regular expression tasks in Python, PHP and Java, with runnable code and a cheat sheet for Go, Rust, C#, Ruby and shell tools, and learn which syntax differences cause most bugs.

Once you know regex in one language, you know about 90 percent of it in all of them. The remaining 10 percent is the API (what you call), the flags (how you spell them) and a handful of syntax features. This lesson solves the same three tasks in Python, PHP and Java: find all matches, capture named parts, and replace with a reference. Then a quick table covers other languages.

The three tasks use the same input so you can compare:

  1. Find every price such as $12.50 in a sentence.
  2. Parse 2025-03-09 into year, month and day by name.
  3. Reformat it to 09/03/2025 with a back-reference.

Python: the re module

Python's API is function-based. Use raw strings (r"...") so backslashes reach the engine unchanged. re.search finds the first match anywhere, re.match only at the start of the string, and re.fullmatch requires the whole string to match, which is the one you want for validation.

Python Python re module

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python quirks to remember: re.match is anchored at the start only (not the end); the function re.sub accepts a function as replacement like JavaScript's replacer; named groups use (?P<name>...); and since Python 3.11 atomic groups (?>...) and possessive quantifiers are available.

PHP: the preg_* functions

PHP uses PCRE. The pattern is a string with delimiters (usually /) followed by flags. Remember that inside a PHP string, single quotes keep backslashes as written, which is why patterns usually use single quotes.

PHP PHP preg functions

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP quirks: functions return 1, 0 or false (on error), so compare with === false when it matters; always add the u modifier for UTF-8 text; and PHP exposes a backtracking limit (pcre.backtrack_limit), so a runaway pattern returns false instead of hanging.

Java: java.util.regex

Java requires doubling backslashes in string literals ("\\d+"), or text blocks. You compile a Pattern, create a Matcher, and ask it for matches. Methods String.matches and replaceAll are shortcuts.

Java Java Pattern and Matcher

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java quirks: matches() is a full match while find() is a search (the opposite of what the names suggest to newcomers); Pattern objects are thread-safe and should be reused, but Matcher objects are not; and use Matcher.quoteReplacement when inserting user text into a replacement.

Cheat sheet for other languages

Language Find all Replace Named group Notes
Go re.FindAllString(s, -1) re.ReplaceAllString(s, "${1}") (?P<n>..) or (?<n>..) RE2: linear time, no backreferences or lookaround
Rust re.find_iter(s) re.replace_all(s, "$1") (?P<n>..) or (?<n>..) regex crate, linear time
C# Regex.Matches(s, p) Regex.Replace(s, p, "$1") (?<n>..) RegexOptions, NonBacktracking, match timeout
Ruby s.scan(/p/) s.gsub(/p/, '\1') (?<n>..) Literals like JavaScript; \A and \z
Perl @m = $s =~ /p/g s/p/$1/g (?<n>..) The origin of PCRE
Swift s.matches(of: /p/) s.replacing(/p/, with: ...) (?<n>..) Typed regex literals
SQL (PostgreSQL) regexp_matches regexp_replace n/a POSIX syntax, ~ and ~* operators

If you read the same idea across languages, note what stays the same: character classes, quantifiers, groups, alternation, anchors. Differences are mostly in the wrapping. More on dialects is in regex flavors and engines, and the command-line versions are in the next lesson.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Which Python function requires the whole string to match the pattern?

    re.match only anchors the start, re.search looks anywhere, and re.fullmatch requires the entire string to match.
  2. 2. Why are PHP regex patterns usually written in single quotes?

    Single-quoted PHP strings do not process most escape sequences, so \d reaches PCRE as written.
  3. 3. What does Java's String.matches do?

    matches() is a full-string match; use Matcher.find() to search within a string.
  4. 4. Which engines guarantee linear matching time but have no backreferences?

    Go's regexp and Rust's regex crate are RE2-style engines built for linear time.

Summary

  • The core syntax is shared; APIs, flags and some features differ per language.
  • Python: raw strings, re.fullmatch for validation, (?P<name>...).
  • PHP: delimiters and modifiers, preg_* functions, the u modifier for UTF-8.
  • Java: doubled backslashes, Pattern/Matcher, matches() versus find().
  • Go and Rust trade features for guaranteed linear time.

Next: Regex on the Command Line