// In this example, a parser is created which can parse and normalize Dutch postcodes. package parsekit_test import ( "fmt" "git.makaay.nl/mauricem/go-parsekit" ) func createPostcodeMatcher() *parsekit.MatcherWrapper { // Easy access to the parsekit definitions. var c, a, m = parsekit.C, parsekit.A, parsekit.M // Matcher functions are created and combined to satisfy these rules: // - A Dutch postcode consists of 4 digits and 2 letters (1234XX). // - The first digit is never a zero. // - A space between letters and digits is optional. // - It is good form to write the letters in upper case. // - It is good form to use a single space between digits and letters. var digitNotZero = c.Except(c.Rune('0'), a.Digit) var pcDigits = c.Seq(digitNotZero, c.Rep(3, a.Digit)) var pcLetter = c.Any(a.ASCIILower, a.ASCIIUpper) var pcLetters = m.ToUpper(c.Seq(pcLetter, pcLetter)) var space = m.Replace(c.Opt(a.Whitespace), " ") var postcode = c.Seq(pcDigits, space, pcLetters) return parsekit.NewMatcher(postcode, "a Dutch postcode") } func Example_dutchPostcodeUsingMatcher() { pcParser := createPostcodeMatcher() for i, input := range []string{ "1234 AB", "2233Ab", "1001\t\tab", "1818ab", "1234", "huh", } { output, err, ok := pcParser.Parse(input) if !ok { fmt.Printf("[%d] Input: %q Error: %s\n", i, input, err) } else { fmt.Printf("[%d] Input: %q Output: %s\n", i, input, output) } } // Output: // [0] Input: "1234 AB" Output: 1234 AB // [1] Input: "2233Ab" Output: 2233 AB // [2] Input: "1001\t\tab" Output: 1001 AB // [3] Input: "1818ab" Output: 1818 AB // [4] Input: "1234" Error: unexpected character '1' (expected a Dutch postcode) // [5] Input: "huh" Error: unexpected character 'h' (expected a Dutch postcode) }