Perl
perl 5- About
- Backtracking engine. The origin of most of the syntax the other dialects share.
- Verified on
- Perl 5.44.0
Offsets are counted in code points. - Found in
- perl
its own documentation - Flags
aasciiiignore casemmultilinenno capturesdotallxverbose
Character classes 15 of 26
| Syntax | Matches | Notes | Try |
|---|---|---|---|
. |
Any characterAny single character. Whether that includes a newline depends on the dialect and its flags. | Does not match a newline. |
test |
[abc]not[^abc] |
One of a set of characters | test | |
[a-z] |
One of a range of characters | test | |
[\]] |
Backslash escape inside a setInside a bracket expression a backslash takes the special meaning from the next character. Where this is missing, a backslash there is a member like any other. | test | |
[\d] |
Class escape inside a setA class escape such as the one for a digit, used as a member of a bracket expression. | test | |
[\D] |
Negated class escape inside a setA negated class escape, such as the one for anything but a digit, used as a member of a bracket expression. | test | |
\d[[:digit:]]not\D |
Digit | Matches the decimal digits of every script, not only |
test |
\w[[:alnum:]_]not\W |
Word characterA letter, a digit or an underscore. | Unicode-aware: matches letters and digits of every script. |
test |
\s[[:space:]]not\S |
Whitespace | Also matches the no-break space. |
test |
\hnot\H |
Horizontal whitespaceA space or a tab, but not a line break. | test | |
\vnot\V |
Vertical whitespaceA line break character of any kind. | test | |
\N |
Any character except newlineNever matches a newline, whatever the flags. | test | |
\R |
Any line break sequenceOne line break, with CR LF taken as a single unit. | test | |
| n/a | Alphabetic character (shorthand) | not in Perl Tried: |
who has it |
| n/a | Lowercase letter (shorthand) | not in Perl Tried: |
who has it |
| n/a | Uppercase letter (shorthand) | not in Perl Tried: |
who has it |
| n/a | Hexadecimal digit (shorthand) | not in Perl Tried: |
who has it |
(?[ X & Y ]) |
Class intersectionCharacters that are in both classes. | test | |
(?[ X - Y ]) |
Class subtractionCharacters in the first class that are not in the second. | test | |
| n/a | Head-of-word character (shorthand)A letter or the underscore: what a word may start with. | not in Perl Tried: |
who has it |
| n/a | Octal digit (shorthand) | not in Perl Tried: |
who has it |
| n/a | Identifier characterA character allowed in an identifier. In Vim the set comes from the 'isident' option. | not in Perl Tried: |
who has it |
| n/a | Keyword characterA character allowed in a keyword. In Vim the set comes from the 'iskeyword' option. | not in Perl Tried: |
who has it |
| n/a | File name characterA character allowed in a file name. In Vim the set comes from the 'isfname' option. | not in Perl Tried: |
who has it |
| n/a | Printable characterA character that is displayed as itself. In Vim the set comes from the 'isprint' option. | not in Perl Tried: |
who has it |
| n/a | Any single byteOne code unit, even where that is part of a longer character, and even a newline. | not in Perl Tried: |
who has it |
Anchors 9 of 14
| Syntax | Matches | Notes | Try |
|---|---|---|---|
^ |
Start of the text or lineStart of the text. With a multiline flag, or always in some dialects, also the start of each line. | test | |
$ |
End of the text or line | Also matches before a newline that ends the text. |
test |
\A |
Start of the textStart of the text only, whatever the flags. | test | |
\z |
End of the textThe very end of the text, whatever the flags. | test | |
\Z |
End of the text, or before a final newline | test | |
\b |
Word boundaryThe position between a word character and a non-word character or an end of the text. | Unicode-aware: a letter outside ASCII is a word character. |
test |
\B |
Not a word boundary | test | |
| n/a | Start of a word | not in Perl Tried: |
who has it |
| n/a | End of a word | not in Perl Tried: |
who has it |
\G |
Where the previous match endedThe position where the last match stopped, or the start of the text for the first match. | test | |
\K |
Reset the start of the matchEverything matched so far is required but left out of the reported match. | test | |
| n/a | Start of symbolBefore a run of word and symbol characters, such as an identifier with an underscore in it. | not in Perl Tried: |
who has it |
| n/a | End of symbol | not in Perl Tried: |
who has it |
| n/a | Cursor positionWhere the editor's cursor is in the buffer being searched. | not in Perl Perl has no editor buffer. The nearest assertion is doc Not run on an engine: taken from the documentation. |
who has it |
Quantifiers 9 of 9
| Syntax | Matches | Notes | Try |
|---|---|---|---|
X? |
Zero or one | test | |
X* |
Zero or more | test | |
X+ |
One or more | test | |
X{N} |
Exactly N times | test | |
X{N,} |
N or more times | test | |
X{,M}X{0,M} |
At most M times | test | |
X{N,M} |
Between N and M times | test | |
X*?X+?X?? |
Lazy quantifierRepeat as few times as possible instead of as many as possible. | test | |
X*+X++X?+ |
Possessive quantifierRepeat as many times as possible and never give any of it back. | test |
Groups and alternation 15 of 16
| Syntax | Matches | Notes | Try |
|---|---|---|---|
(X) |
Capture group | test | |
(?:X) |
Non-capturing group | test | |
(?<NAME>X)(?'NAME'X)(?P<NAME>X) |
Named capture group | test | |
X|Y |
Alternation | Takes the first alternative that matches, even when a later one is longer. |
test |
\N\gN\g{N} |
Backreference by numberThe same text that capture group N matched. | test | |
\k<NAME>\k'NAME'\k{NAME}\g{NAME}(?P=NAME) |
Backreference by name | test | |
\g-N\g{-N} |
Backreference by relative positionThe group N groups back from here. | test | |
(?>X) |
Atomic groupOnce the group has matched, the engine never tries a different way to match it. | test | |
(?|X) |
Branch reset groupEvery alternative numbers its capture groups from the same start. | test | |
(?(N)X|Y) |
ConditionalMatch one pattern if a capture group took part, otherwise another. | test | |
(?R) |
Recurse into the whole pattern | test | |
(?N) |
Call a group as a subroutineMatch the pattern of capture group N again at this point, not the text it matched. | test | |
(?#TEXT) |
Comment | test | |
| n/a | Explicitly numbered groupA capture group that is given its number, whatever its position. | not in Perl Tried: |
who has it |
(?-N) |
Call an earlier group by relative positionMatch the pattern of the Nth group before this point again. | test | |
(?+N) |
Call a later group by relative positionMatch the pattern of the Nth group after this point here as well. | test |
Lookaround 4 of 4
Escape sequences 14 of 16
| Syntax | Matches | Notes | Try |
|---|---|---|---|
\. |
Escape a special characterTake the next character literally. | test | |
| n/a | Quote a run of textTake everything between the markers literally. | not in Perl Tried: |
who has it |
\t |
Tab | test | |
\n |
Newline | test | |
\r |
Carriage return | test | |
\f |
Form feed | test | |
| n/a | Vertical tab | not in Perl Tried: |
who has it |
\a |
Alert (bell) | test | |
[\b] |
Backspace | test | |
\e |
Escape character | test | |
\cC |
Control characterThe control character typed as Ctrl plus the letter. | test | |
\OOO\o{OOO} |
Character by octal code | test | |
\xHH |
Character by two-digit hex code | test | |
\x{HHHH}\N{U+HHHH} |
Character by Unicode code point | test | |
\N{CHARNAME} |
Character by Unicode name | test | |
\0\x00\o{0}\000 |
NUL characterThe character with code 0. | A backslash followed by doc Not run on an engine: taken from the documentation. |
Flags and modes 13 of 14
| Syntax | Matches | Notes | Try |
|---|---|---|---|
flag i(?i) |
Ignore case | test | |
flag m(?m) |
MultilineThe line anchors also match at the start and end of every line. | test | |
flag s(?s) |
Dot matches newline | test | |
flag x(?x) |
Free spacingWhitespace in the pattern is ignored, and a # starts a comment. | test | |
| n/a | Swap greedy and lazy | not in Perl Tried: |
who has it |
(?FLAGS) |
Turn a mode on inside the patternApplies from this point to the end of the pattern or the enclosing group. | test | |
(?FLAGS) |
Turn a mode on in the middle of the patternThe mode applies from where the flag stands, not to what comes before it. Some dialects accept an inline flag only at the very start of the pattern. | test | |
(?FLAGS:X) |
Turn a mode on for one group | test | |
(?-FLAGS) |
Turn a mode off inside the pattern | test | |
(?a) |
ASCII-only classesMakes the shorthand classes such as the digit class match ASCII characters only. | Doubling the letter, |
test |
(?u) |
Unicode classesMakes the shorthand classes such as the digit class match by Unicode property. | test | |
(?n) |
No automatic capturePlain parentheses group without capturing; only named groups capture. | test | |
/PATTERN/l(?l)(?l:PATTERN) |
Locale-dependent classesClasses and case folding follow the current locale. | Uses the locale in effect when the match runs. Of multi-byte locales only UTF-8 ones are supported, code points above 255 are always treated as Unicode, and the documentation recommends doc Not run on an engine: taken from the documentation. |
|
/PATTERN/d(?d)(?d:PATTERN) |
Native character set rulesPerl's rules from before 5.14: the pattern and the text decide between native and Unicode rules. | Native rules unless the string or pattern is UTF-8 flagged or the pattern uses a Unicode-only construct. It is the default when no pragma selects another set; the documentation advises the doc Not run on an engine: taken from the documentation. |
POSIX named classes 1 of 1
| Syntax | Matches | Notes | Try |
|---|---|---|---|
[[:NAME:]]not[^[:NAME:]] |
POSIX named classA named set of characters, written inside a bracket expression. | 14 names: Not here: |
test |
POSIX collating symbols 0 of 2
| Syntax | Matches | Notes | Try |
|---|---|---|---|
| n/a | Equivalence classEvery character that collates the same as X in the current locale. | not in Perl Tried: |
who has it |
| n/a | Collating symbol by nameA single collating element, written by its POSIX name inside a bracket expression. | not in Perl Tried: |
who has it |
Unicode properties 4 of 4
| Syntax | Matches | Notes | Try |
|---|---|---|---|
\p{NAME}not\P{NAME} |
Unicode general category | 36 names: |
test |
\p{NAME}not\P{NAME} |
Unicode script | Goes by Script_Extensions: it also matches a mark shared between scripts, such as a combining accent. 8 names: |
test |
\X |
Extended grapheme clusterOne character as a reader sees it: a base character with its combining marks. | test | |
\b{NAME} |
Unicode text boundaryA boundary as Unicode's segmentation rules define it, which is not the same as a boundary between word and non-word characters. | 5 names: |
test |
Syntax table classes 0 of 2
| Syntax | Matches | Notes | Try |
|---|---|---|---|
| n/a | Syntax classA character with the given role in the syntax table: whitespace, word, symbol, punctuation, a bracket. | not in Perl Tried: |
who has it |
| n/a | Character categoryA character in one of the editor's character categories, most of which are scripts. | not in Perl Tried: |
who has it |
Host language 15 of 15
| Syntax | Matches | Notes | Try |
|---|---|---|---|
/PATTERN/m{PATTERN}qr/PATTERN/ |
Pattern in source codeHow a pattern is written where it is used: a regex literal with its delimiters, or the kind of string the documentation recommends. | With a leading doc Not run on an engine: taken from the documentation. |
|
/PATTERN/FLAGSqr/PATTERN/FLAGS |
Flags outside the patternWhere flags such as case-insensitive go when they are not written inside the pattern. | Letters after the closing delimiter: doc Not run on an engine: taken from the documentation. |
|
qr/PATTERN/ |
Compile once and reuseBuild a pattern object and keep it. | The returned value can be matched directly ( doc Not run on an engine: taken from the documentation. |
|
TEXT =~ /PATTERN/ |
Search a textTest whether the pattern matches anywhere in a text, or get the first match. | Searches anywhere in the string; true or false in scalar context, the list of captured groups in list context. Without doc Not run on an engine: taken from the documentation. |
|
TEXT =~ /PATTERN/g |
Every matchGet all the matches that do not overlap. | In list context returns every match, or every captured group if the pattern has groups. In scalar context each evaluation finds the next match. doc Not run on an engine: taken from the documentation. |
|
TEXT =~ s/PATTERN/REPLACEMENT/TEXT =~ s/PATTERN/REPLACEMENT/gTEXT =~ s/PATTERN/REPLACEMENT/r |
Replace matches | Changes TEXT in place and replaces the first match; doc Not run on an engine: taken from the documentation. |
|
$NUM$+{NAME} |
Captured group, in codeRead what a group captured after a match. | Set by the last successful match in the current dynamic scope; a failed match does not reset them. doc Not run on an engine: taken from the documentation. |
|
$NUM${NUM}$+{NAME} |
Captured group, in replacement text | Write doc Not run on an engine: taken from the documentation. |
|
$&${^MATCH} |
Whole match, in replacement text | The replacement is interpolated like a double-quoted string, so a literal dollar sign is written doc Not run on an engine: taken from the documentation. |
|
m?PATTERN? |
Match once until resetA match operator that succeeds one time only, until it is reset. | Matches only once between calls to doc Not run on an engine: taken from the documentation. |
|
TEXT =~ /PATTERN/gc |
Keep the position after a failed matchIn a repeated search, a failed match does not send the next one back to the start. | A failed doc Not run on an engine: taken from the documentation. |
|
/PATTERN/p |
Keep the matched text for laterAsk for the matched text, and the text around it, to be saved. | Makes doc Not run on an engine: taken from the documentation. |
|
/PATTERN/o |
Interpolate and compile only onceA pattern literal with variables in it is built the first time and never again. | Perl never recompiles the pattern, so later changes to interpolated variables go unnoticed. The documentation calls doc Not run on an engine: taken from the documentation. |
|
(?{CODE})(*{CODE}) |
Code inside a patternHost-language code that runs when the match reaches it. | Zero-width, always succeeds, and stores its return value in doc Not run on an engine: taken from the documentation. |
|
(??{CODE}) |
Pattern computed during the matchCode inside the pattern whose result is matched as a pattern at that point. | The return value is compiled as a pattern if it is a string, or used as is if it is a doc Not run on an engine: taken from the documentation. |
Every row was run on the engine named above, except the rows marked doc: those describe something no regex engine can run by itself, such as the syntax of the host language, and are taken from the documentation they link to. For a row marked "not in Perl", the spelling other dialects use was tried and the line starting "Tried" says what it did here; that shows the spelling does not work, not that no other spelling exists.