rx

package module
v0.0.0-...-44ab1bf Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2022 License: MIT Imports: 2 Imported by: 0

README

rx

Expressive regex builder

Documentation

Overview

Package rx provides an expressive way to build regular expressions.

Index

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

func (r Regex) AnyOf(rxs ...Regex) Regex

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

func (r Regex) Capture(rxs ...Regex) Regex

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

func (r Regex) CaptureName(name string, rxs ...Regex) Regex

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

func (r Regex) In(rxs ...Regex) Regex

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

func (r Regex) NOrMore(n int, rx Regex) Regex

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

func (r Regex) NOrMoreLazy(n int, rx Regex) Regex

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

func (r Regex) NTimes(n int, rx Regex) Regex

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

func (r Regex) NTimesLazy(n int, rx Regex) Regex

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

func (r Regex) NUpToM(n, m int, rx Regex) Regex

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

func (r Regex) NUpToMLazy(n, m int, rx Regex) Regex

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

func (r Regex) NotIn(rxs ...Regex) Regex

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

func (r Regex) OneOrMore(rx Regex) Regex

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

func (r Regex) OneOrMoreLazy(rx Regex) Regex

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

func (r Regex) Then(rxs ...Regex) Regex

Then appends the given regexes to the current one.

Example
fmt.Println(Regex("a").Then("b", Any))
Output:

ab.

func (Regex) WithFlags

func (r Regex) WithFlags(flags Flags, rxs ...Regex) Regex

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

func (r Regex) ZeroOrMore(rx Regex) Regex

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

func (r Regex) ZeroOrMoreLazy(rx Regex) Regex

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)*?

func (Regex) ZeroOrOne

func (r Regex) ZeroOrOne(rx Regex) Regex

ZeroOrOne appends the given regex with a "one or more" quantifier ("?"), prefer more.

Example
fmt.Println(Regex("a").ZeroOrOne("b"))
fmt.Println(Regex("a").ZeroOrOne("abc"))
Output:

ab?
a(?:abc)?

func (Regex) ZeroOrOneLazy

func (r Regex) ZeroOrOneLazy(rx Regex) Regex

ZeroOrOneLazy appends the given regex with a "one or more" quantifier ("?"), prefer more.

Example
fmt.Println(Regex("a").ZeroOrOneLazy("b"))
fmt.Println(Regex("a").ZeroOrOneLazy("abc"))
Output:

ab??
a(?:abc)??

Jump to

Keyboard shortcuts

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