Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท bytes, unicode and text

Go regexp package

Reference for Go regexp: compile patterns with Compile and MustCompile, RE2 syntax, flags, named groups, linear-time matching and what the engine leaves out.

The package regexp implements regular expressions using the RE2 syntax. A pattern is compiled once into a *regexp.Regexp, and that value has the methods for matching, finding, replacing and splitting. The engine guarantees matching time linear in the size of the input, so a hostile pattern or input cannot cause catastrophic backtracking.

The price of that guarantee is a smaller feature set: there are no backreferences (\1) and no lookahead or lookbehind. For everything else you normally need, including named groups, non-greedy quantifiers, Unicode classes and flags, the package is enough.

Go
re := regexp.MustCompile(`(\d+)-(\d+)`)
fmt.Println(re.FindStringSubmatch("pages 10-25")) // [10-25 10 25]

Try it

Go Compile, match and replace

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go Named groups, flags and Unicode

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Package-level functions (compile or match once):

Go
func Compile(expr string) (*Regexp, error)
func MustCompile(str string) *Regexp
func CompilePOSIX(expr string) (*Regexp, error)
func MatchString(pattern string, s string) (matched bool, err error)
func Match(pattern string, b []byte) (matched bool, err error)
func QuoteMeta(s string) string
Function Description
Compile Parses the pattern. Returns an error for invalid syntax.
MustCompile Same, but panics on error. Use it for package-level variables with constant patterns.
CompilePOSIX POSIX ERE syntax with leftmost-longest matching. Rarely needed.
MatchString / Match Compile and match in one call. Convenient but recompiles every time.
QuoteMeta Escapes all regular-expression metacharacters so user input matches literally.

Commonly used syntax (RE2):

Syntax Meaning
. \d \w \s \b Any character except newline; digit; word character; space; word boundary. All ASCII-based.
[a-z] [^0-9] [[:alpha:]] Character classes.
\p{L} \p{Greek} \P{N} Unicode categories and scripts, and their negations.
* + ? {n,m} Greedy repetition. Append ? for non-greedy (*?).
(x) (?:x) (?P<n>x) (?<n>x) Capturing group, non-capturing group, named group (both spellings).
(?i) (?s) (?m) (?U) Flags: ignore case, dot matches newline, multi-line anchors, swap greedy and non-greedy.
^ $ \A \z Start and end of text (or of a line with (?m)).

Not supported: backreferences, lookahead, lookbehind, atomic groups and possessive quantifiers. Compile returns an error such as invalid escape sequence for them.

Usage notes

  • Use raw string literals (backticks) for patterns so you write \d instead of "\\d".
  • Compile once. Compiling is far more expensive than matching. Put var re = regexp.MustCompile(...) at package level or build it in a constructor, not inside a loop or a request handler.
  • A *Regexp is safe for concurrent use by multiple goroutines (the Longest method is the exception while it is being called).
  • Leftmost-first matching is the default, like Perl: among alternatives the first one that matches wins. re.Longest() switches the compiled expression to leftmost-longest.
  • Matching is on bytes, interpreted as UTF-8. Indexes returned by the Index methods are byte offsets into the input, which is what you need for slicing.
  • \d, \w, \s, \b are ASCII only. Use \p{L}, \p{N} and friends for Unicode letters and digits.
  • Do not build patterns by concatenating user input without regexp.QuoteMeta.
  • When a plain string function is enough, use it. strings.Contains, Cut, Fields and HasPrefix are simpler and faster for fixed text.
  • Linear time is not constant time. A very long input with a heavy pattern still takes time proportional to its length times the pattern size; limit the input length for untrusted data.