liveBook · Manning

Regular expression

As explained in Learn Go with Pocket-Sized Projects

A regular expression is a pattern notation for matching and extracting text according to specified character and sequence rules.

A regular expression is a pattern notation for matching and extracting text according to character and sequence rules. In Go, the regexp package supports tests such as validating identifiers produced by randomized code. section 8.5.2 section 8.7

Finding and normalizing a generated ID

To test JSON containing a random alphanumeric ID, compile `.+"id":"([a-zA-Z0-9]+)".+`, call `FindStringSubmatch`, verify that one capture was found, replace the captured value with a fixed string, and compare the normalized JSON. The character class permits uppercase letters, lowercase letters, and digits; the parentheses capture the ID. section 8.5.2

Use regular expressions to:

  • Validate that an identifier contains the expected number and kinds of characters. section 8.5.2
  • Match variable output without requiring randomized values to be identical across test runs. section 8.5.2
  • Document complicated patterns with comments because compact expressions can become difficult to read. section 8.7

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Defines character classes, captures, and submatch extraction through one JSON example, but does not develop regexp syntax broadly.
Examples
Demonstrates extracting an alphanumeric ID from JSON and validating generated IDs with regexp assertions.
What it assumes
Assumes Go testing and JSON-handler knowledge while teaching pattern matching through a concrete identifier test.
What you can do after
Diagnose and test variable text by matching patterns, extracting captures, and normalizing randomized values before comparison.
How it connects
Connects regular expressions with Go testing, JSON responses, HTTP handlers, and randomized identifier generation.

Also explained in

Compare how 27 books explain regular expression →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.