regex

package
v0.0.0-...-1dacd80 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2022 License: Apache-2.0, MIT Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Captures

func Captures[T stream.Token](re *regexp.Regexp) parser.Func[T, [][]T]

Captures matches `re` on the input by running `Find[String]SubmatchIndex` on the input. / Returns the captures of the first match and consumes the input up until the end of that match.

Example
package main

import (
	"fmt"
	"regexp"

	"github.com/flier/gocombine/pkg/parser/regex"
	"github.com/flier/gocombine/pkg/parser/to"
)

func main() {
	digits := to.StringSlice(regex.Captures[rune](regexp.MustCompile("([a-z]+):([0-9]+)")))

	fmt.Println(digits([]rune("test:123 field:456 ")))
	fmt.Println(digits([]rune("test:123 :456 ")))

}
Output:

[test:123 test 123] [32 102 105 101 108 100 58 52 53 54 32] <nil>
[test:123 test 123] [32 58 52 53 54 32] <nil>

func CapturesMany

func CapturesMany[T stream.Token](re *regexp.Regexp) parser.Func[T, [][][]T]

CapturesMany matches `re` on the input by running `FindAll[String]SubmatchIndex` on the input. / Returns all captures until the end of the last match.

Example
package main

import (
	"fmt"
	"regexp"

	"github.com/flier/gocombine/pkg/parser/combinator"
	"github.com/flier/gocombine/pkg/parser/regex"
)

func main() {
	digits := combinator.Map(
		regex.CapturesMany[rune](regexp.MustCompile("([a-z]+)?:([0-9]+)")),
		func(captured [][][]rune) (r [][]string) {
			r = make([][]string, len(captured))

			for i, sub := range captured {
				r[i] = make([]string, len(sub))

				for j, s := range sub {
					r[i][j] = string(s)
				}
			}

			return
		},
	)

	fmt.Println(digits([]rune("test:123 field:456 ")))
	fmt.Println(digits([]rune("test:123 :456 ")))

}
Output:

[[test:123 test 123] [field:456 field 456]] [32] <nil>
[[test:123 test 123] [:456  456]] [32] <nil>

func Find

func Find[T stream.Token](re *regexp.Regexp) parser.Func[T, []T]

Find matches `re` on the input by running `find` on the input and returns the first match. Consumes all input up until the end of the first match.

Example
package main

import (
	"fmt"
	"regexp"

	"github.com/flier/gocombine/pkg/parser/regex"
	"github.com/flier/gocombine/pkg/parser/to"
)

func main() {
	digits := to.String(regex.Find[rune](regexp.MustCompile("^[0-9]+")))

	fmt.Println(digits([]rune("123 456 ")))

	digits2 := to.String(regex.Find[byte](regexp.MustCompile("[0-9]+")))

	fmt.Println(digits2([]byte("123 456 ")))
	fmt.Println(digits2([]byte("abcd 123 456 ")))

}
Output:

123 [32 52 53 54 32] <nil>
123 [32 52 53 54 32] <nil>
123 [32 52 53 54 32] <nil>

func FindMany

func FindMany[T stream.Token](re *regexp.Regexp) parser.Func[T, [][]T]

FindMany matches `re` on the input by running `FindAll` on the input. / Returns all matches until the end of the last match.

Example
package main

import (
	"fmt"
	"regexp"

	"github.com/flier/gocombine/pkg/parser/regex"
	"github.com/flier/gocombine/pkg/parser/to"
)

func main() {
	digits2 := to.StringSlice(regex.FindMany[rune](regexp.MustCompile("[0-9]+")))

	fmt.Println(digits2([]rune("123 456 ")))
	fmt.Println(digits2([]rune("abcd 123 456 ")))
	fmt.Println(digits2([]rune("abcd")))

}
Output:

[123 456] [32] <nil>
[123 456] [32] <nil>
[] [97 98 99 100] <nil>

func Match

func Match[T stream.Token](re *regexp.Regexp) parser.Func[T, bool]

Match matches `re` on the input returning the entire input if it matches. Never consumes any input.

Example
package main

import (
	"fmt"
	"regexp"

	"github.com/flier/gocombine/pkg/parser/regex"
)

func main() {
	p := regex.Match[rune](regexp.MustCompile("[:alpha:]+"))

	fmt.Println(p([]rune("abc123")))
	fmt.Println(p([]rune("Bingo!")))
	fmt.Println(p(nil))

}
Output:

true [97 98 99 49 50 51] <nil>
false [66 105 110 103 111 33] <nil>
false [] <nil>

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL