# Regex Syntax Differences in GoogleTest Death Tests: POSIX vs Windows and macOS

> Understand regex syntax differences in GoogleTest death tests. Discover limited simple regex on Windows/macOS versus full POSIX support on Linux/BSD.

- Repository: [Google/googletest](https://github.com/google/googletest)
- Tags: deep-dive
- Published: 2026-08-30

---

**GoogleTest death tests use the full POSIX extended regular expression engine via `<regex.h>` on Linux and BSD systems, but fall back to a deliberately limited simple regex engine on Windows and macOS that lacks support for unions, grouping, brackets, and explicit repetition counts.**

When writing cross-platform death tests in the [google/googletest](https://github.com/google/googletest) framework, understanding the **regex syntax support differences between POSIX systems and Windows/Mac** is critical to avoiding runtime failures. The library selects between two distinct implementations at compile time, leading to incompatible pattern capabilities across platforms.

## Platform-Specific Regex Implementations

GoogleTest branches its death-test string matching logic based on the `GTEST_USES_POSIX_RE` and `GTEST_USES_SIMPLE_RE` macros defined in the porting layer.

### POSIX-Compliant Systems (Linux, BSD)

On POSIX-compliant systems, GoogleTest delegates to the system `<regex.h>` library. As documented in [`googletest/include/gtest/gtest-death-test.h`](https://github.com/google/googletest/blob/main/googletest/include/gtest/gtest-death-test.h) at lines 108-109, this provides **POSIX extended regular expressions (POSIX ERE)** with full capabilities including:

- Unions (`a|b`)
- Grouping (`(ab)`)
- Character classes (`[a-z]`)
- Repetition quantifiers (`{5,7}`)
- All standard POSIX escape sequences

This implementation lives in the system headers and requires no additional code within GoogleTest itself.

### Windows and macOS (Non-POSIX Platforms)

On Windows, macOS, and other non-POSIX platforms, GoogleTest compiles its own **simple regex engine** implemented in [`googletest/include/gtest/internal/gtest-death-test-internal.h`](https://github.com/google/googletest/blob/main/googletest/include/gtest/internal/gtest-death-test-internal.h). The selection logic resides in `googletest/src/gtest-port.cc` (using the `GTEST_USES_SIMPLE_RE` path).

According to the commentary in [`gtest-death-test.h`](https://github.com/google/googletest/blob/main/gtest-death-test.h) at lines 111-118, this engine intentionally implements only a **portable subset** of regex syntax. Attempting to use unsupported constructs results in a runtime failure with an error message indicating the invalid construct.

## Supported Regex Features on Non-POSIX Platforms

When targeting Windows or macOS, limit patterns to the following simple regex subset:

- **Literal characters** and escaped literals (`\c`)
- **Common escape classes** (`\d`, `\w`, `\s`, etc.)
- **Wildcard** `.` (matches any character except `\n`)
- **Basic quantifiers** `?`, `*`, and `+` applied to a single atom only
- **Anchors** `^` and `$` (matching start and end of the entire string)
- **Concatenation** (`xy`)

The following POSIX features are **explicitly unsupported** on non-POSIX platforms:

- Union/alternation (`x|y`)
- Grouping parentheses (`(xy)`)
- Bracket expressions (`[xy]`)
- Repetition counts (`x{5,7}`)

## Writing Portable Death Test Patterns

The following examples demonstrate how platform-specific syntax affects death test assertions.

On POSIX systems, you can use full extended regex features:

```cpp
// POSIX (Linux, BSD) – full POSIX-ERE features available
TEST(FooDeathTest, PosixRegex) {
  // Uses character class [0-9] and repetition count {3}
  ASSERT_DEATH(MyFunction(),
               "Error: .*\\[0-9]{3}\\] encountered");
}

```

The same pattern fails on Windows or macOS because the simple engine cannot parse character classes or repetition counts:

```cpp
// Windows/macOS – this will FAIL with regex error
TEST(FooDeathTest, SimpleRegexBroken) {
  ASSERT_DEATH(MyFunction(),
               "Error: .*\\[0-9]{3}\\] encountered");  // ❌ Runtime error
}

```

Rewrite patterns using only the portable subset to ensure cross-platform compatibility:

```cpp
// Portable solution – works on all platforms
TEST(FooDeathTest, SimpleRegexCorrect) {
  // Replace [0-9] with \d and {3} with explicit repetition
  ASSERT_DEATH(MyFunction(),
               "Error: .*\\d\\d\\d\\] encountered");  // ✅ Universal support
}

```

## Key Source Files Controlling Regex Behavior

Understanding the implementation requires examining these specific files in the GoogleTest repository:

- **[`googletest/include/gtest/gtest-death-test.h`](https://github.com/google/googletest/blob/main/googletest/include/gtest/gtest-death-test.h)** – Contains the public API and platform-specific documentation at lines 108-118 describing the regex capabilities.
- **[`googletest/include/gtest/internal/gtest-death-test-internal.h`](https://github.com/google/googletest/blob/main/googletest/include/gtest/internal/gtest-death-test-internal.h)** – Implements the simple regex engine used when `GTEST_USES_SIMPLE_RE` is defined.
- **`googletest/src/gtest-port.cc`** – Defines the platform detection logic that selects between `GTEST_USES_POSIX_RE` (using `<regex.h>`) and `GTEST_USES_SIMPLE_RE`.
- **[`docs/advanced.md`](https://github.com/google/googletest/blob/main/docs/advanced.md)** – User-facing documentation referencing both POSIX and simple regex syntax limitations.

## Summary

- POSIX systems (Linux, BSD) utilize the native `<regex.h>` library, providing full POSIX extended regex capabilities including unions, groups, and repetition counts.
- Windows and macOS rely on GoogleTest's simple regex engine, which supports only literals, basic escapes, `.`, `?`, `*`, `+`, `^`, `$`, and concatenation.
- Patterns using `|`, `()`, `[]`, or `{m,n}` syntax trigger runtime failures on non-POSIX platforms.
- For maximum portability, rewrite complex patterns using escaped character classes (`\d`, `\w`) and explicit repetition instead of quantifier braces.

## Frequently Asked Questions

### What regex syntax does GoogleTest use on Linux?

On Linux and other POSIX-compliant systems, GoogleTest uses the system `<regex.h>` library, which implements **POSIX extended regular expressions (ERE)**. This provides full support for character classes, grouping, alternation, and repetition counts as documented in [`gtest-death-test.h`](https://github.com/google/googletest/blob/main/gtest-death-test.h).

### Why do my death test patterns fail on Windows but work on Linux?

Windows uses GoogleTest's **simple regex engine**, which does not support POSIX features like square brackets (`[0-9]`) or curly brace quantifiers (`{3}`). Patterns must be rewritten using only literal characters, backslash escapes (`\d`, `\w`), and basic quantifiers (`*`, `+`, `?`).

### Which regex features should I avoid for cross-platform death tests?

Avoid **unions** (`|`), **grouping** (`()`), **bracket expressions** (`[]`), and **explicit repetition counts** (`{m,n}`). These constructs are parsed by the POSIX engine on Linux but cause runtime errors on Windows and macOS where only the simple engine is available.

### Where is the regex engine selection implemented in the GoogleTest source?

The selection logic resides in `googletest/src/gtest-port.cc`, which defines either `GTEST_USES_POSIX_RE` or `GTEST_USES_SIMPLE_RE` based on platform capabilities. The simple engine implementation is found in [`googletest/include/gtest/internal/gtest-death-test-internal.h`](https://github.com/google/googletest/blob/main/googletest/include/gtest/internal/gtest-death-test-internal.h), while the public API documentation appears in [`googletest/include/gtest/gtest-death-test.h`](https://github.com/google/googletest/blob/main/googletest/include/gtest/gtest-death-test.h) at lines 108-118.