Documentation ¶
Overview ¶
Package typedregexp matches regular expressions into structs.
Regular expressions are specified as a template string (ala text/template), and a struct value whose fields must all be strings. Each field on the struct must contain a valid regular expression. The template string then has each reference to each field replaced with a capture group that matches the corresponding sub-expression in the field.
POSIX regular expressions are not supported. regexp.CompilePOSIX can't handle named capture groups.
The returned TypedRegexp can be used to fill a struct.
type Values struct { Name string Age string } re, err := Compile("Hi, I'm {{.Name}}. I'm {{.Age}} years old!", Values{ Name: `\w+`, Age: `\d+`, }) if err != nil { log.Fatal(err) } var values Values re.Find("Hi, I'm Sam. I'm 20 years old!", &values)
values is now:
{Name:Sam Age:20}
See Examples for more features.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type TypedRegexp ¶
type TypedRegexp struct {
// contains filtered or unexported fields
}
TypedRegexp is the representation of a compiled regular expression that fills a struct with the values of its submatches.
func Compile ¶
func Compile(patternTemplate string, subExpressions interface{}) (*TypedRegexp, error)
Compile creates a TypedRegexp from a regular expression pattern string that uses the text/template package to refer to fields in a struct. subExpressions must be a struct with only exported string fields. Each field should contain the regex to match into that field.
func MustCompile ¶
func MustCompile(pattern string, subExpressions interface{}) *TypedRegexp
MustCompile is the same as Compile, but panics on error.
func (*TypedRegexp) Find ¶
func (r *TypedRegexp) Find(s string, values interface{}) (found bool)
Find sets the fields of values to the matches of the patterns specified for them in Compile. values must be a pointer to the same type of struct as used in Compile. Returns true if there is at least 1 field match.
Fields for which no match is found will not be modified, so you can specify default values by just setting fields on values before passing it to this method.
Example ¶
package main import ( "fmt" "log" ) type Values struct { Name string Age string } func main() { re, err := Compile("Hi, I'm {{.Name}}. I'm {{.Age}} years old!|I'm {{.Name}}, I'm {{.Age}}.", Values{ Name: `\w+`, Age: `\d+`, }) if err != nil { log.Fatal(err) } var values Values if re.Find("Hi, I'm Sam. I'm 20 years old!", &values) { fmt.Printf("%+v", values) } else { fmt.Println("No match.") } }
Output: {Name:Sam Age:20}
Example (DefaultValues) ¶
package main import ( "fmt" "log" ) type ValuesDefaults struct { Name string Age string } func main() { re, err := Compile("Hi, I'm {{.Name}}. I'm {{.Age}} years old!|I'm {{.Name}}, I'm {{.Age}}.", ValuesDefaults{ Name: `\w+`, Age: `\d+`, }) if err != nil { log.Fatal(err) } values := ValuesDefaults{ Name: "Jane Doe", Age: "-1", } re.Find("I'll ask the questions.", &values) fmt.Printf("%+v", values) }
Output: {Name:Jane Doe Age:-1}
Example (PartialMatch) ¶
package main import ( "fmt" "log" ) type ValuesPartialMatch struct { Name string Age string } func main() { re, err := Compile("Hi, I'm {{.Name}}.( I'm {{.Age}} years old!)?", ValuesPartialMatch{ Name: `\w+`, Age: `\d+`, }) if err != nil { log.Fatal(err) } var values ValuesPartialMatch re.Find("Hi, I'm Sam.", &values) fmt.Printf("%+v", values) }
Output: {Name:Sam Age:}
func (*TypedRegexp) FindAll ¶
func (r *TypedRegexp) FindAll(s string, valuesSlice interface{}) (n int)
FindAll finds the first len(values) matches of the regex in s. valuesSlice must be a slice of structs (or pointers to structs) of the same type as passed to Compile. Nil elements are skipped. Returns the number of matches found. Note that not all valuesSlice[:n] may have been set from the regexp, if some of the matches didn't match on any of the fields.
Calling FindAll with a slice of len 1 is basically same as passing a pointer to that struct to Find().
Example ¶
package main import "fmt" const data = ` The Beatles - Abbey Road Aidan Knight - Each Other The Dø - Shake Shook Shaken ` type Album struct { Artist string Title string } func main() { parser := MustCompile("(?m)^{{.Artist}} - {{.Title}}$", Album{ Artist: `.+`, Title: `.+`, }) albums := make([]Album, 20) n := parser.FindAll(data, albums) for _, album := range albums[:n] { fmt.Println(album) } }
Output: {The Beatles Abbey Road} {Aidan Knight Each Other} {The Dø Shake Shook Shaken}
func (*TypedRegexp) String ¶
func (r *TypedRegexp) String() string