Documentation ¶
Overview ¶
Package rx provides an expressive way to build regular expressions.
Index ¶
- type Flags
- type Regex
- func (r Regex) AnyOf(rxs ...Regex) Regex
- func (r Regex) Capture(rxs ...Regex) Regex
- func (r Regex) CaptureName(name string, rxs ...Regex) Regex
- func (r Regex) In(rxs ...Regex) Regex
- func (r Regex) NOrMore(n int, rx Regex) Regex
- func (r Regex) NOrMoreLazy(n int, rx Regex) Regex
- func (r Regex) NTimes(n int, rx Regex) Regex
- func (r Regex) NTimesLazy(n int, rx Regex) Regex
- func (r Regex) NUpToM(n, m int, rx Regex) Regex
- func (r Regex) NUpToMLazy(n, m int, rx Regex) Regex
- func (r Regex) NotIn(rxs ...Regex) Regex
- func (r Regex) OneOrMore(rx Regex) Regex
- func (r Regex) OneOrMoreLazy(rx Regex) Regex
- func (r Regex) Then(rxs ...Regex) Regex
- func (r Regex) WithFlags(flags Flags, rxs ...Regex) Regex
- func (r Regex) ZeroOrMore(rx Regex) Regex
- func (r Regex) ZeroOrMoreLazy(rx Regex) Regex
- func (r Regex) ZeroOrOne(rx Regex) Regex
- func (r Regex) ZeroOrOneLazy(rx Regex) Regex
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Flags ¶
type Flags uint
Flags changes the meaning of the regex.
const ( // CaseInsensitive makes the regex case insensitive (flag "i" set). CaseInsensitive Flags = 1 << iota // CaseSensitive makes the regex case sensitive (flag "i" cleared, // default behavior). CaseSensitive // MultiLine makes StartOfText and EndOfText match the beginning and // the end of each line (flag "m" set). MultiLine // SingleLine makes StartOfText and EndOfText match the beginning and // the end of the whole text (flag "m" cleared, default behavior). SingleLine // AnyNL makes Any match new lines(flag "s" set). AnyNL // AnyNoNL makes Any not match new lines (flag "s" cleared, default // behavior). AnyNoNL // Ungreedy makes the quantifiers match the shortest text possible // (flag "U" set). Ungreedy // Greedy makes the quantifiers match the longest text possible (flag // "U" cleared, default behavior). Greedy )
type Regex ¶
type Regex string
Regex represents a regular expression.
const ( // Any matches any character ("."). Any Regex = "." )
func (Regex) AnyOf ¶
AnyOf appends an alternative to the regex. The resulting alternative matches any of the given regexes.
Example ¶
fmt.Println(Regex("a").AnyOf("b", "c"))
Output: a(?:b|c)
func (Regex) Capture ¶
Capture appends a capture group to the regexes. The regexes given as arguments are just concatenated.
Example ¶
fmt.Println(Regex("a").Capture("a", "b"))
Output: a(ab)
func (Regex) CaptureName ¶
CaptureName appends a named capture group to the regexes. The regexes given as arguments are just concatenated.
Example ¶
fmt.Println(Regex("a").CaptureName("foo", "a", "b"))
Output: a(?P<foo>ab)
func (Regex) In ¶
In generates a character class composed by the concatenation of all arguments.
Example ¶
fmt.Println(Regex("a").In("0-9", "a-z"))
Output: a[0-9a-z]
func (Regex) NOrMore ¶
NOrMore appends the given regex with an minimum number of repeats ("{N,}"), prefer more.
Example ¶
fmt.Println(Regex("a").NOrMore(3, "b")) fmt.Println(Regex("a").NOrMore(3, "abc"))
Output: ab{3,} a(?:abc){3,}
func (Regex) NOrMoreLazy ¶
NOrMoreLazy appends the given regex with an minimum number of repeats ("{N,}"), prefer more.
Example ¶
fmt.Println(Regex("a").NOrMoreLazy(3, "b")) fmt.Println(Regex("a").NOrMoreLazy(3, "abc"))
Output: ab{3,}? a(?:abc){3,}?
func (Regex) NTimes ¶
NTimes appends the given regex with an exact number of repeats ("{N}").
Example ¶
fmt.Println(Regex("a").NTimes(3, "b")) fmt.Println(Regex("a").NTimes(3, "abc"))
Output: ab{3} a(?:abc){3}
func (Regex) NTimesLazy ¶
NTimesLazy appends the given regex with an exact number of repeats ("{N}").
Example ¶
fmt.Println(Regex("a").NTimesLazy(3, "b")) fmt.Println(Regex("a").NTimesLazy(3, "abc"))
Output: ab{3}? a(?:abc){3}?
func (Regex) NUpToM ¶
NUpToM appends the given regex with an minimum and maximum number of repeats ("{N,M}"), prefer more.
Example ¶
fmt.Println(Regex("a").NUpToM(3, 5, "b")) fmt.Println(Regex("a").NUpToM(3, 5, "abc"))
Output: ab{3,5} a(?:abc){3,5}
func (Regex) NUpToMLazy ¶
NUpToMLazy appends the given regex with an minimum and maximum number of repeats ("{N,M}"), prefer more.
Example ¶
fmt.Println(Regex("a").NUpToMLazy(3, 5, "b")) fmt.Println(Regex("a").NUpToMLazy(3, 5, "abc"))
Output: ab{3,5}? a(?:abc){3,5}?
func (Regex) NotIn ¶
NotIn generates a negated character class composed by the concatenation of all arguments.
Example ¶
fmt.Println(Regex("a").NotIn("0-9", "a-z"))
Output: a[^0-9a-z]
func (Regex) OneOrMore ¶
OneOrMore appends the given regex with a "one or more" quantifier ("+"), prefer more.
Example ¶
fmt.Println(Regex("a").OneOrMore("b")) fmt.Println(Regex("a").OneOrMore("abc"))
Output: ab+ a(?:abc)+
func (Regex) OneOrMoreLazy ¶
OneOrMoreLazy appends the given regex with a "one or more" quantifier ("+"), prefer more.
Example ¶
fmt.Println(Regex("a").OneOrMoreLazy("b")) fmt.Println(Regex("a").OneOrMoreLazy("abc"))
Output: ab+? a(?:abc)+?
func (Regex) Then ¶
Then appends the given regexes to the current one.
Example ¶
fmt.Println(Regex("a").Then("b", Any))
Output: ab.
func (Regex) WithFlags ¶
WithFlags appends a non-capturing group with the given flags set to the regex. The regexes given as arguments are just concatenated.
Example ¶
fmt.Println(Regex("a").WithFlags(AnyNL|CaseSensitive, "a", "b"))
Output: a(?s-i:ab)
func (Regex) ZeroOrMore ¶
ZeroOrMore appends the given regex with a "zero or more" quantifier ("*"), prefer more.
Example ¶
fmt.Println(Regex("a").ZeroOrMore("b")) fmt.Println(Regex("a").ZeroOrMore("abc"))
Output: ab* a(?:abc)*
func (Regex) ZeroOrMoreLazy ¶
ZeroOrMoreLazy appends the given regex with a "zero or more" quantifier ("*"), prefer more.
Example ¶
fmt.Println(Regex("a").ZeroOrMoreLazy("b")) fmt.Println(Regex("a").ZeroOrMoreLazy("abc"))
Output: ab*? a(?:abc)*?