--- name: regex compilation errors --- # Regex compilation errors Every diagnostic on this page comes from the pattern compiler. A regular expression is compiled before it runs, and a malformed pattern dies at that point - before the match is ever attempted. The errors are all fatal (**F**); trap them with `eval` if you build patterns from untrusted input: ```perl my $re = eval { qr/$user_pattern/ }; die "bad pattern: $@" unless $re; ``` Upstream Perl appends a ` marked by <-- HERE in m/.../` suffix that points at the offending position. PetaPerl reports the same errors; the exact position-marker formatting may differ. ## Common structural errors ### `` `Unmatched ) in regex` `` **(F)** A `)` in the pattern has no matching `(`. Usually a typo or an unescaped literal parenthesis. To match a literal `)`, escape it as `\)` or put it in a character class `[)]`. ### `` `Quantifier follows nothing in regex` `` **(F)** A quantifier (`*`, `+`, `?`, `{n,m}`) appeared with nothing in front of it to repeat - for example `/*abc/` or `/(?:)+/` on an empty group. Quantify a literal `*` by escaping it: `/\*/`. ### `` `Unterminated group` `` **(F)** A `(` opened a group that the pattern never closes. Balance the parentheses. ### `` `Unterminated character class` `` **(F)** A `[` opened a character class with no closing `]`. To match a literal `]`, place it first in the class (`[]x]`) or escape it (`[\]x]`). ### `` `Unterminated comment group` `` **(F)** A `(?#...)` comment group was opened but not closed with `)`. ### `` `Unterminated conditional pattern` `` **(F)** A `(?(condition)yes|no)` construct was left unclosed. ### `` `Unterminated group name` `` **(F)** A named-group construct (`(?...)`, `\k`) ran to the end of the pattern without closing its name delimiter. ### `` `Unrecognized group type` `` **(F)** The characters after `(?` do not form a group type PetaPerl knows. Check the `(?...)` spelling against the regex reference. ### `` `Unexpected end of character class` `` **(F)** The pattern ended in the middle of a `[...]` class. ### `` `Unexpected end of escape sequence` `` **(F)** A backslash escape was cut off by the end of the pattern. ## Character-class and range errors ### `` `Invalid [] range "%s" in regex` `` **(F)** A character range inside `[...]` runs backwards (high to low), as in `[z-a]`. Order the endpoints low to high, or escape the `-` to match it literally. ### `` `Invalid range endpoint in character class` `` **(F)** One end of a `-` range is not a single character (for example a multi-character escape used as an endpoint). ### `` `POSIX class [:%s:] unknown` `` **(F)** A `[:name:]` POSIX class names a class that does not exist. The valid names are `alpha`, `digit`, `alnum`, `space`, `upper`, `lower`, `punct`, `print`, `graph`, `cntrl`, `xdigit`, `blank`, `word`, and `ascii`. A bare `[:alpha:]` outside another `[...]` is also an error - POSIX classes only work *inside* a character class: `[[:alpha:]]`. ### `` `Empty POSIX character class name` `` **(F)** A `[::]` with nothing between the colons. ## Control-character and numeric escapes ### `` `Missing control char` `` **(F)** A `\c` escape was not followed by the character it should control, as in a pattern ending in `\c`. ### `` `Missing control character after \c` `` **(F)** Same cause as above, from a different point in the compiler: `\c` needs one following character. ### `` `Invalid control character '\c%s'` `` **(F)** The character after `\c` is not one `\c` accepts. ### `` `Invalid octal escape` `` **(F)** A `\0`/`\o` octal escape is malformed. ### `` `Invalid octal digit in \o{}` `` **(F)** A digit inside `\o{...}` is not 0–7. ### `` `Expected '{' after \o` `` **(F)** `\o` must be followed by `{`, as in `\o{17}`. ### `` `Expected '}' in \o{} `` and `` `Invalid Unicode code point in \o{}` `` **(F)** A `\o{...}` escape is unterminated or names a code point outside the valid range. ### `` `Invalid hex escape` `` / `` `Invalid hex digit` `` / `` `Expected '}' in hex escape` `` **(F)** A `\x` escape is malformed: a non-hex digit, or an unterminated `\x{...}`. ## `\g`, `\k`, and group references ### `` `\g0 is not allowed` `` and `` `\g{0} is not allowed` `` **(F)** Group 0 is the whole match and cannot be back-referenced. Reference a real capture group (`\g1`, `\g{name}`). ### `` `Invalid group number in \g` `` / `` `Invalid group number in \g{}` `` **(F)** The number after `\g` is not a valid group reference. ### `` `Expected digit or '{' after \g` `` / `` `Expected '}' after \g{` `` **(F)** `\g` must be followed by a digit, a sign, or `{name}`. ### `` `Reference to nonexistent group number %d` `` **(F)** A `\g`/`\1`-style back-reference names a group number the pattern does not contain. ### `` `Reference to nonexistent group (relative offset %d)` `` **(F)** A relative back-reference (`\g{-2}`) points past the groups that exist. ### `` `Reference to nonexistent named group '%s'` `` **(F)** A `\k` or `\g{name}` references a named group that was never defined. ### `` `Reference to nonexistent or invalid group` `` **(F)** A group reference could not be resolved to any group. ### `` `Expected '<' or '\'' after \k` `` / `` `Expected '}' after \k{` `` **(F)** A named back-reference `\k` must be written `\k`, `\k'name'`, or `\k{name}`. ## Named groups ### `` `Empty group name` `` **(F)** A named group `(?<>...)` or reference has empty name brackets. ### `` `Group name must not start with a digit` `` **(F)** Group names follow identifier rules; they may not begin with a digit. ## `\N` and Unicode escapes ### `` `\N in character class requires braces` `` **(F)** Inside `[...]`, `\N` must be written `\N{...}`; the bare "any non-newline" form is not allowed in a class. ### `` `\N{NAME} not supported in character class` `` **(F)** A named-character `\N{NAME}` escape is not permitted inside a character class. ### `` `Expected '}' after \N{` `` / `` `Empty \N{U+} escape` `` **(F)** A `\N{...}` escape is unterminated or `\N{U+}` has no hex digits. ### `` `Invalid hex in \N{U+...}` `` / `` `Invalid Unicode code point in \N{U+...}` `` **(F)** The hex inside `\N{U+...}` is malformed or names a code point outside the Unicode range. ### `` `Invalid Unicode code point` `` **(F)** A code-point escape names a value above the Unicode maximum (`0x10FFFF`). ### `` `Empty Unicode property name` `` / `` `Unknown Unicode property name` `` **(F)** A `\p{...}` / `\P{...}` property escape is empty or names a property PetaPerl does not recognize. ## `(?...)` extended constructs ### `` `Invalid flag after (?^` `` / `` `Invalid flag character` `` **(F)** A flag-setting group `(?flags)` or `(?^flags:...)` contains a character that is not a valid pattern modifier. ### `` `Expected '{' after (??` `` **(F)** A `(??{ code })` postponed-subpattern construct is malformed. ### `` `Expected '<', '=', or '>' after (?P` `` **(F)** The Python-style `(?P...)` / `(?P=name)` / `(?P>name)` construct is malformed. ### `` `Expected DEFINE after (?(` `` / `` `Expected ')' after DEFINE` `` **(F)** A `(?(DEFINE)...)` block is malformed. ### `` `Switch condition not recognized in regex` `` / `` `Invalid lookaround condition` `` **(F)** The condition in a `(?(...)...)` conditional is not a recognized form (a group number, a lookaround, `R`, or `DEFINE`). ### `` `Expected ')' after condition` `` family **(F)** A conditional construct's condition was not closed with `)`. The variants - `` `Expected ')' after (?(R...)` ``, `` `Expected ')' after code block condition` ``, `` `Expected ')' after lookahead condition` ``, `` `Expected ')' after lookbehind condition` ``, `` `Expected '=' or '!' after (?(<` `` - all report the same shape of error at different condition types. ## Quantifier braces ### `` `Expected '}' after quantifier` `` / `` `Expected '}' or ',' in quantifier` `` **(F)** A `{n,m}` quantifier is unterminated or contains an unexpected character. ## Lookbehind ### `` `Variable length lookbehind not implemented` `` **(F)** A lookbehind `(?<=...)` / `(?