preg_last_error

preg_last_error(): int

Returns the error code of the most recent PCRE failure.

C C23

C23
#include <regex.h>
#include <stdio.h>
int main(void) {
    regex_t re;
    if (regcomp(&re, "(", REG_EXTENDED) != 0) { // nonzero return = compile failure
        printf("regcomp error");
    } else {
        regfree(&re);
    }
    return 0;
}
Output
regcomp error

C++ C++26

C++26
#include <iostream>
#include <regex>
int main() {
    try {
        std::regex re("("); // invalid pattern throws std::regex_error
        std::cout << "ok";
    } catch (const std::regex_error &) {
        std::cout << "regex_error";
    }
    return 0;
}
Output
regex_error

C# v11.0.100

v11.0.100
try
{
    var re = new System.Text.RegularExpressions.Regex("(");
    Console.WriteLine("ok");
}
catch (ArgumentException) // .NET 5+ actually throws RegexParseException (a subclass)
{
    Console.WriteLine("ArgumentException");
}
Output
ArgumentException

Go v1.27.4

v1.27.4
package main

import (
    "fmt"
    "regexp"
)

func main() {
    _, err := regexp.Compile("(")
    if err != nil {
        fmt.Println("regexp.Compile error")
    }
}
Output
regexp.Compile error

Java v27.0.1

v27.0.1
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class Main {
    public static void main(String[] args) {
        try {
            Pattern.compile("(");
        } catch (PatternSyntaxException e) {
            System.out.println("PatternSyntaxException");
        }
    }
}
Output
PatternSyntaxException

JavaScript v26.13.1

v26.13.1
try {
  new RegExp("(");
  console.log("ok");
} catch (e) {
  console.log(e.name);
}
Output
SyntaxError

PHP v8.6.0

v8.6.0
$r = @preg_match("(", "abc");
echo preg_last_error();
Output
1

Python v3.15.2

v3.15.2
import re

try:
    re.compile("(")
    print("ok")
except re.error:
    print("re.error")
Output
re.error

Rust v1.99.1

v1.99.1
fn main() {
    // Regex::new returns Err for an invalid pattern (Result instead of a global error state)
    match regex::Regex::new("(") {
        Ok(_) => println!("ok"),
        Err(_) => println!("Regex::new error"),
    }
}
Output
Regex::new error

TypeScript v26.13.1

v26.13.1
try {
  new RegExp("(");
  console.log("ok");
} catch (e) {
  console.log((e as Error).name); // TS: the catch variable defaults to unknown
}
Output
SyntaxError

Language differences

C: the regcomp return value is itself the error code (not global); a failed match is signaled by regexec returning REG_NOMATCH.

C++: an invalid pattern throws immediately — std::regex_error.

C#: throws RegexParseException (a subclass of ArgumentException).

Go: failure reported through the return value — regexp.Compile yields an error, with MustCompile panicking for hard-coded patterns.

Java: an invalid pattern throws immediately — PatternSyntaxException.

JavaScript: throws SyntaxError.

PHP: keeps a global "most recent PCRE error" state — preg_last_error() returns the code (0 = PREG_NO_ERROR, 1 = PREG_INTERNAL_ERROR for pattern compilation failure, 2 = PREG_BACKTRACK_LIMIT_ERROR, 6 = PREG_BAD_UTF8_ERROR when an invalid UTF-8 sequence meets a u-flagged pattern), typically queried after silencing the warning with @.

Python: most other languages have no such global state — an invalid pattern throws immediately (re.error).

Rust: failure reported through the return value — Regex::new yields an Err.

TypeScript: same as JS — throws SyntaxError.