/regex rosetta stone/i

Go

package regexp, RE2 syntax
About
Automaton, no backtracking: match time is linear in the input, and constructs that need backtracking do not exist.
Verified on
Go 1.27.4
Offsets are counted in bytes.
Found in
go regexp
its own documentation
Flags
U ungreedyi ignore casem multilines dotall
Character classes 9 of 26
SyntaxMatchesNotesTry
. 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

ASCII digits only: 0 to 9.

test
\w[[:alnum:]_]not\W Word characterA letter, a digit or an underscore.

ASCII only: A to Z, a to z, 0 to 9 and _.

test
\s[[:space:]]not\S Whitespace

Does not match the no-break space.

Does not match a vertical tab.

test
n/a Horizontal whitespaceA space or a tab, but not a line break. not in Go

Tried: \h does not compile.

who has it
n/a Vertical whitespaceA line break character of any kind. not in Go

Tried: \v compiles, and matches as plain text or not at all.

who has it
n/a Any character except newlineNever matches a newline, whatever the flags. not in Go

Tried: \N does not compile.

who has it
n/a Any line break sequenceOne line break, with CR LF taken as a single unit. not in Go

Tried: \R does not compile.

who has it
n/a Alphabetic character (shorthand) not in Go

Tried: \a compiles, and matches as plain text or not at all.

who has it
n/a Lowercase letter (shorthand) not in Go

Tried: \l does not compile.

who has it
n/a Uppercase letter (shorthand) not in Go

Tried: \u does not compile.

who has it
n/a Hexadecimal digit (shorthand) not in Go

Tried: \h does not compile.

who has it
n/a Class intersectionCharacters that are in both classes. not in Go

Tried: [a-z&&[^a-m]] compiles, and matches as plain text or not at all.

who has it
n/a Class subtractionCharacters in the first class that are not in the second. not in Go

Tried: [a-z&&[^aeiou]] compiles, and matches as plain text or not at all.

who has it
n/a Head-of-word character (shorthand)A letter or the underscore: what a word may start with. not in Go

Tried: \h does not compile.

who has it
n/a Octal digit (shorthand) not in Go

Tried: \o does not compile.

who has it
n/a Identifier characterA character allowed in an identifier. In Vim the set comes from the 'isident' option. not in Go

Tried: \i does not compile.

who has it
n/a Keyword characterA character allowed in a keyword. In Vim the set comes from the 'iskeyword' option. not in Go

Tried: \k does not compile.

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 Go

Tried: \f compiles, and matches as plain text or not at all.

who has it
n/a Printable characterA character that is displayed as itself. In Vim the set comes from the 'isprint' option. not in Go

Tried: \p does not compile.

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 Go

Tried: \C does not compile.

who has it
Anchors 6 of 14
SyntaxMatchesNotesTry
^ 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

Matches only at the very end, not before a final newline.

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
n/a End of the text, or before a final newline not in Go

Tried: a\Z does not compile.

who has it
\b Word boundaryThe position between a word character and a non-word character or an end of the text.

ASCII word characters only: a letter outside ASCII counts as a non-word character.

test
\B Not a word boundary test
n/a Start of a word not in Go

Tried: \<foo compiles, and matches as plain text or not at all.

who has it
n/a End of a word not in Go

Tried: foo\> compiles, and matches as plain text or not at all.

who has it
n/a Where the previous match endedThe position where the last match stopped, or the start of the text for the first match. not in Go

Tried: \Ga does not compile.

who has it
n/a Reset the start of the matchEverything matched so far is required but left out of the reported match. not in Go

Tried: foo\Kbar does not compile.

who has it
n/a Start of symbolBefore a run of word and symbol characters, such as an identifier with an underscore in it. not in Go

Tried: \_<b compiles, and matches as plain text or not at all.

who has it
n/a End of symbol not in Go

Tried: a\_> compiles, and matches as plain text or not at all.

who has it
n/a Cursor positionWhere the editor's cursor is in the buffer being searched. not in Go

doc Not run on an engine: taken from the documentation.

who has it
Quantifiers 8 of 9
SyntaxMatchesNotesTry
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{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
n/a Possessive quantifierRepeat as many times as possible and never give any of it back. not in Go

Tried: a*+b does not compile.

who has it
Groups and alternation 4 of 16
SyntaxMatchesNotesTry
(X) Capture group test
(?:X) Non-capturing group test
(?<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/a Backreference by numberThe same text that capture group N matched. not in Go

Tried: (a)\1 does not compile.

who has it
n/a Backreference by name not in Go

Tried: (?<n>a)\k<n> does not compile.

who has it
n/a Backreference by relative positionThe group N groups back from here. not in Go

Tried: (a)\g-1 does not compile.

who has it
n/a Atomic groupOnce the group has matched, the engine never tries a different way to match it. not in Go

Tried: (?>a+)b does not compile.

who has it
n/a Branch reset groupEvery alternative numbers its capture groups from the same start. not in Go

Tried: (?|(a)|(b)) does not compile.

who has it
n/a ConditionalMatch one pattern if a capture group took part, otherwise another. not in Go

Tried: (<)?a(?(1)>|!) does not compile.

who has it
n/a Recurse into the whole pattern not in Go

Tried: \((?:a|(?R))\) does not compile.

who has it
n/a Call a group as a subroutineMatch the pattern of capture group N again at this point, not the text it matched. not in Go

Tried: (a|b)(?1) does not compile.

who has it
n/a Comment not in Go

Tried: a(?#note)b does not compile.

who has it
n/a Explicitly numbered groupA capture group that is given its number, whatever its position. not in Go

(?<2>a) compiles, but as a group named 2 that is group number 1.

Tried: \(?2:a\) compiles, and matches as plain text or not at all.

who has it
n/a Call an earlier group by relative positionMatch the pattern of the Nth group before this point again. not in Go

Tried: (a|b)(?-1) does not compile.

who has it
n/a Call a later group by relative positionMatch the pattern of the Nth group after this point here as well. not in Go

Tried: (?+1)(a|b) does not compile.

who has it
Lookaround 0 of 4
SyntaxMatchesNotesTry
n/a LookaheadWhat follows must match X, but is not part of the match. not in Go

Tried: a(?=c) does not compile.

who has it
n/a Negative lookahead not in Go

Tried: a(?!b) does not compile.

who has it
n/a LookbehindWhat comes before must match X, but is not part of the match. not in Go

Tried: (?<=c)a does not compile.

who has it
n/a Negative lookbehind not in Go

Tried: (?<!b)a does not compile.

who has it
Escape sequences 12 of 16
SyntaxMatchesNotesTry
\. Escape a special characterTake the next character literally. test
\QTEXT\E Quote a run of textTake everything between the markers literally. test
\t Tab test
\n Newline test
\r Carriage return test
\f Form feed test
\v Vertical tab test
\a Alert (bell) test
n/a Backspace not in Go

Tried: [\b] does not compile.

who has it
n/a Escape character not in Go

Tried: \e does not compile.

who has it
n/a Control characterThe control character typed as Ctrl plus the letter. not in Go

Tried: \cA does not compile.

who has it
\OOO Character by octal code test
\xHH Character by two-digit hex code test
\x{HHHH} Character by Unicode code point test
n/a Character by Unicode name not in Go

Tried: \N{DIGIT ONE} does not compile.

who has it
\x00\000\0 NUL characterThe character with code 0.

\0 is an octal escape that takes up to two more octal digits, so write \000 or \x00 before a digit. Go strings are byte sequences, so the text can contain NUL.

doc Not run on an engine: taken from the documentation.

Flags and modes 8 of 14
SyntaxMatchesNotesTry
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
n/a Free spacingWhitespace in the pattern is ignored, and a # starts a comment. not in Go

Tried: (?x)a b does not compile.

who has it
flag U(?U) Swap greedy and lazy test
(?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
n/a ASCII-only classesMakes the shorthand classes such as the digit class match ASCII characters only. not in Go

\d matches ASCII digits only, with no mode to set.

Tried: (?a)\d does not compile.

who has it
n/a Unicode classesMakes the shorthand classes such as the digit class match by Unicode property. not in Go

Tried: (?u)\d does not compile.

who has it
n/a No automatic capturePlain parentheses group without capturing; only named groups capture. not in Go

Tried: (?n)(a)(?<x>b) does not compile.

who has it
n/a Locale-dependent classesClasses and case folding follow the current locale. not in Go

The flags are i, m, s and U only. \d, \s, \w and the [[:name:]] classes are documented as ASCII-only.

doc Not run on an engine: taken from the documentation.

who has it
n/a Native character set rulesPerl's rules from before 5.14: the pattern and the text decide between native and Unicode rules. not in Go

doc Not run on an engine: taken from the documentation.

who has it
POSIX named classes 1 of 1
SyntaxMatchesNotesTry
[[:NAME:]]not[^[:NAME:]] POSIX named classA named set of characters, written inside a bracket expression.

14 names: alnum alpha ascii blank cntrl digit graph lower print punct space upper word xdigit

Not here: nonascii multibyte

test
POSIX collating symbols 0 of 2
SyntaxMatchesNotesTry
n/a Equivalence classEvery character that collates the same as X in the current locale. not in Go

Tried: [[=a=]] compiles, and matches as plain text or not at all.

who has it
n/a Collating symbol by nameA single collating element, written by its POSIX name inside a bracket expression. not in Go

Tried: [[.SOH.]] compiles, and matches as plain text or not at all.

who has it
Unicode properties 2 of 4
SyntaxMatchesNotesTry
\p{NAME}not\P{NAME} Unicode general category

36 names: L Ll Lm Lo Lt Lu C Cc Cf Cn Co M Mc Me Mn N Nd Nl No P Pc Pd Pe Pi Pf Po Ps S Sc Sk Sm So Z Zl Zp Zs

test
\p{NAME}not\P{NAME} Unicode script

8 names: Latin Greek Cyrillic Hebrew Arabic Han Hiragana Thai

test
n/a Extended grapheme clusterOne character as a reader sees it: a base character with its combining marks. not in Go

Tried: \X does not compile.

who has it
n/a 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. not in Go

Tried: e\b{gcb} compiles, and matches as plain text or not at all.

who has it
Syntax table classes 0 of 2
SyntaxMatchesNotesTry
n/a Syntax classA character with the given role in the syntax table: whitespace, word, symbol, punctuation, a bracket. not in Go

Tried: \s- compiles, and matches as plain text or not at all.

who has it
n/a Character categoryA character in one of the editor's character categories, most of which are scripts. not in Go

Tried: \ca does not compile.

who has it
Host language 8 of 15
SyntaxMatchesNotesTry
`PATTERN`"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.

No regex literal; the package example uses a raw string in back quotes, where backslashes are not special and a back quote cannot appear. In an interpreted "..." string every backslash and double quote must be escaped.

doc Not run on an engine: taken from the documentation.

n/a Flags outside the patternWhere flags such as case-insensitive go when they are not written inside the pattern. not in Go

regexp.Compile and regexp.MustCompile take only the pattern string. Flags go inside the pattern as a leading group, for example (?i)PATTERN.

doc Not run on an engine: taken from the documentation.

who has it
regexp.MustCompile(PATTERN)regexp.Compile(PATTERN) Compile once and reuseBuild a pattern object and keep it.

Compile returns (*Regexp, error); MustCompile panics on an invalid pattern and suits package-level variables.

doc Not run on an engine: taken from the documentation.

regexp.MatchString(PATTERN, TEXT)regexp.MustCompile(PATTERN).MatchString(TEXT)regexp.MustCompile(PATTERN).FindString(TEXT) Search a textTest whether the pattern matches anywhere in a text, or get the first match.

Not anchored: a match anywhere in TEXT counts. FindString returns the empty string both for an empty match and for no match.

doc Not run on an engine: taken from the documentation.

regexp.MustCompile(PATTERN).FindAllString(TEXT, -1)regexp.MustCompile(PATTERN).FindAllStringSubmatch(TEXT, -1) Every matchGet all the matches that do not overlap.

The last argument caps the number of matches; a negative value returns all of them.

doc Not run on an engine: taken from the documentation.

regexp.MustCompile(PATTERN).ReplaceAllString(TEXT, REPLACEMENT)regexp.MustCompile(PATTERN).ReplaceAllLiteralString(TEXT, REPLACEMENT) Replace matches

Replaces every match; the package has no replace-first method. ReplaceAllLiteralString inserts REPLACEMENT without expanding $.

doc Not run on an engine: taken from the documentation.

m[NUM]m[re.SubexpIndex("NAME")] Captured group, in codeRead what a group captured after a match.

m is the slice returned by re.FindStringSubmatch(TEXT); m[0] is the whole match. SubexpIndex returns -1 when no group has that name.

doc Not run on an engine: taken from the documentation.

$NUM${NUM}$NAME${NAME} Captured group, in replacement text

The unbraced name is taken as long as possible: $1x means ${1x} and $10 means ${10}. Write ${1}0 to follow group 1 with a digit.

doc Not run on an engine: taken from the documentation.

$0${0} Whole match, in replacement text

Submatch 0 is the whole match. A literal $ is written $$.

doc Not run on an engine: taken from the documentation.

n/a Match once until resetA match operator that succeeds one time only, until it is reset. not in Go

doc Not run on an engine: taken from the documentation.

who has it
n/a Keep the position after a failed matchIn a repeated search, a failed match does not send the next one back to the start. not in Go

A Regexp stores no search position; every method takes the whole input.

doc Not run on an engine: taken from the documentation.

who has it
n/a Keep the matched text for laterAsk for the matched text, and the text around it, to be saved. not in Go

No such option. The Index method variants return byte offsets, from which the text before and after the match can be sliced.

doc Not run on an engine: taken from the documentation.

who has it
n/a Interpolate and compile only onceA pattern literal with variables in it is built the first time and never again. not in Go

No pattern literal and no interpolation. The documented idiom is a package-level var re = regexp.MustCompile(PATTERN), compiled once at init time.

doc Not run on an engine: taken from the documentation.

who has it
n/a Code inside a patternHost-language code that runs when the match reaches it. not in Go

doc Not run on an engine: taken from the documentation.

who has it
n/a Pattern computed during the matchCode inside the pattern whose result is matched as a pattern at that point. not in Go

doc Not run on an engine: taken from the documentation.

who has it

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 Go", 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.