Regular Expressions: RegEx101 versus AutoHotkey

Differences Between AHK RegEx and RegEx101

We like to use RegEx101 for testing our Regular Expressions in AutoHotkey.

It’s a great tool however there are some differences between AutoHotkey and Reg101.  We’ve outlined the “bigies” below.

If you’re new to Regular Expressions, you might want to check out our Intro to RegEx Course

Overview

AutoHotkey (AHK) uses the PCRE (Perl-Compatible Regular Expressions) engine — specifically an older PCRE1 (v8.x) — while regex101 defaults to PCRE2 (used in modern PHP).
They’re closely related, but differ in defaults, features, and option handling, which can cause subtle mismatches.

If you select “PCRE (PHP < 7.3)” on regex101, that’s the closest match to AutoHotkey’s engine.

AutoHotkey RegEx Rules

You can learn “excaly” how RegEx in AutoHotkey works by studying this page


AHK Regex versus RegEx101: What to watch out for

AHK Regex versus RegEx101: What to watch out for


1. Regex Flavor and Engine Version

FeatureAutoHotkeyregex101
EnginePCRE v8.x (PCRE1)PCRE2 by default
Unicode / Line HandlingDefaults to Windows CRLF; can override via (*ANYCRLF)Defaults to LF (\n)
Advanced SyntaxSome PCRE2 features not supportedFull PCRE2 feature set

Notable Differences:
AHK’s PCRE1 is more lenient about odd syntax (like empty group names or stray hyphens), while regex101’s PCRE2 enforces stricter validation and modern Unicode handling.


⚙️ 2. Option / Modifier Syntax

ConceptAutoHotkeyregex101
Where options goInside pattern start → i)abcAfter slashes → /abc/gi or inline (?i)
Common optionsi), m), s), x)Same meanings
Turn off options) closes options(?-i) syntax
Special AHK optionsO) = Object mode, P) = Position moden/a

AHK also supports extra newline control tokens like (*ANYCRLF) and (*BSR_ANYCRLF).


3. Global Matching

  • regex101 / PCRE: “Global” (/g) means find all matches automatically.
  • AutoHotkey:
    • RegExMatch() returns only the first match.
    • To get all matches, loop manually and update the start position.
    • RegExReplace() replaces all matches by default.

pos := 1
while pos := RegExMatch(haystack, "pattern", m, pos + (pos ? StrLen(m[0]) : 0))
    MsgBox m[0]

4. Output & Return Differences

Actionregex101AutoHotkey
Shows matchesAll at onceOne per call (loop for more)
Captured groupsVisual list of groupsm[1], m[2], or m["name"]
Return valueVisual onlyReturns position (1-based) or 0 if no match

In AHK v2, RegExMatch() returns a match object with properties like .Pos, .Len, and .Name.


5. Capturing Groups & Backreferences

  • Backreferences \1, \2 work normally inside the regex pattern itself (for example, (\w+)\s+\1).
  • When using RegExReplace() in AutoHotkey, replacement backreferences use dollar syntax — e.g. $1, $2 — not backslashes.
  • Named groups like (?P<Name>...) or (?<Name>...) are supported in AHK v2 and can be accessed as m["Name"].
  • Edge Cases:
    • Backrefs \8 or \9 behave differently — PCRE1 (AHK) may treat them as errors or literals depending on context, while PCRE2 (regex101) is stricter.
    • Empty or duplicate group names are tolerated in AHK (PCRE1) but rejected in regex101’s PCRE2 flavor.

Summary:
Use \1, \2 for backreferences inside the pattern,
and $1, $2 for captured groups used in RegExReplace() replacement strings.


6. Escaping and String Handling in AHK

  • AHK uses backtick (`) as an escape in string literals.
  • Regex strings often need extra escaping — e.g. "\\d+" or '\\d+'.
  • \r, \n, and \\ must be handled carefully.
  • Literal backticks in regex must be escaped as ``.

This is a major reason regexes from regex101 may fail when copied directly into AHK scripts.


7. Line Break and \R Behavior

  • In AHK, . doesn’t match newlines unless you use s) (dotall).
  • \R matches only CR, LF, or CRLF by default.
  • Use (*ANYCRLF) or (*ANY) to include full Unicode line breaks.
  • regex101 defaults to broader newline support (\n-based).

8. Performance, Limits, and Internal Behavior

  • AHK caches the 100 most recent compiled regexes.
  • Deep recursion or heavy backtracking can trigger runtime errors like “recursion too deep.”
  • regex101’s sandbox has fewer execution limits.
  • AHK’s memory and PCRE limits are compiled in — not configurable at runtime.

9. Miscellaneous Engine Differences

  • No variable-length lookbehind in AHK (PCRE1).
  • Some POSIX class syntax (like [[:punct:]b]) behaves differently.
  • Quantifier and \Q\E quirks differ slightly.
  • \p{} Unicode classes work inconsistently unless Unicode mode is enabled (default in v2).

⚡ 10. Practical Tips When Moving Between regex101 and AHK

✅ On regex101:

  • Select “PCRE (PHP < 7.3)”.
  • Avoid PCRE2-only features.
  • Remember: no /g flag in AHK — use loops instead.

✅ In AHK Code:

  • Prefix options (e.g., i)pattern).
  • Escape backslashes/backticks properly.
  • Loop RegExMatch() for multiple results.
  • RegExReplace() is global by default.
  • Be explicit about newline handling.

Summary Table

Categoryregex101 (PCRE2)AutoHotkey (PCRE1)
EnginePCRE2PCRE v8.x
Global matching/g flagLoop manually
Option placement/pattern/gii)pattern
Default newline\n\r\n
\R rangeAll Unicode newlinesCR, LF, CRLF
Backtick handlingNoneBacktick = escape
Match outputAll at onceOne per call
Version strictnessStricterMore lenient
Named groupsSupportedSupported (v2)
Recursion limitsGenerousLimited
CalloutsShown onlySupported (callback)

In Short

Regex in AutoHotkey is mostly PCRE-compatible but wrapped with AHK-specific syntax and runtime quirks.

  • No global flag → you must loop
  • Option prefixes differ
  • AHK escape rules can trip you up
  • Line endings and Unicode differ slightly
  • Backreferences inside patterns use \1; replacement backrefs use $1.

Otherwise, 95% of regex patterns behave identically between regex101 and AHK.

Comments are closed.