re

package module
v0.0.0-...-f7eb535 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2017 License: Apache-2.0 Imports: 3 Imported by: 0

README

Golang Regular Expressions for Humans

This module is inspired in a similar project for javascript.

Example:

import (
	. "github.com/dakerfp/re"
)

re := Regex(
	Group("dividend", Digits),
	Then("/"),
	Group("divisor", Digits),
)

m = re.FindSubmatch("4/3")
fmt.Println(m[1]) // > 4
fmt.Println(m[2]) // > 3

the equivalent regexp would be:

regexp.MustCompile("(\\d+)/(\\d+)")

which is far more cryptic.

Another good example is the following regex (limited) to parse URLs:

import . "github.com/dakerfp/re"

re := re.Regex(
	StartOfLine,
	Then("http"),
	Maybe("s"),
	Then("://"),
	Maybe("www"),
	AtLeastOne(AnythingBut(' ')),
	EndOfLine,
)

which corresponds to

regexp.MustCompile("^http[s]?://(www)?[^\\ ]+$"),

Contributing

Fill issues for discussions and request, pull request for fixes and new features. License is Apache 2.0.

TODO List

  • Create Docs
  • Validate API
  • Add more tests

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	StartOfLine = &syntax.Regexp{
		Op: syntax.OpBeginLine,
	}
	EndOfLine = &syntax.Regexp{
		Op: syntax.OpEndLine,
	}
	StartOfText = &syntax.Regexp{
		Op: syntax.OpBeginText,
	}
	EndOfText = &syntax.Regexp{
		Op: syntax.OpEndText,
	}
	Digit = &syntax.Regexp{
		Op:   syntax.OpCharClass,
		Rune: digits,
	}
	Period = &syntax.Regexp{
		Op:   syntax.OpLiteral,
		Rune: []rune{'.'},
	}
	Digits = &syntax.Regexp{
		Op:  syntax.OpPlus,
		Sub: []*syntax.Regexp{Digit},
	}
	Lowercase = &syntax.Regexp{
		Op:   syntax.OpCharClass,
		Rune: lowercaseAlpha,
	}
	Uppercase = &syntax.Regexp{
		Op:   syntax.OpCharClass,
		Rune: uppercaseAlpha,
	}
	Alpha = &syntax.Regexp{
		Op:   syntax.OpCharClass,
		Rune: alpha,
	}
	Alphanum = &syntax.Regexp{
		Op:   syntax.OpCharClass,
		Rune: alphanum,
	}
	Anything = &syntax.Regexp{
		Op: syntax.OpAnyChar,
	}
	Word = &syntax.Regexp{
		Op:  syntax.OpPlus,
		Sub: []*syntax.Regexp{Alphanum},
	}
	Newline = &syntax.Regexp{
		Op:   syntax.OpLiteral,
		Rune: []rune("\n"),
	}
	Whitespace = &syntax.Regexp{
		Op: syntax.OpPlus,
		Sub: []*syntax.Regexp{
			&syntax.Regexp{
				Op:   syntax.OpCharClass,
				Rune: whitespaces,
			},
		},
	}
)

Functions

func AnythingBut

func AnythingBut(args ...rune) *syntax.Regexp

func AtLeastOne

func AtLeastOne(sub ...*syntax.Regexp) *syntax.Regexp

func CompileRegex

func CompileRegex(subs ...*syntax.Regexp) (*regexp.Regexp, error)

func Either

func Either(sub ...interface{}) *syntax.Regexp

func Group

func Group(name string, sub ...*syntax.Regexp) *syntax.Regexp

Groups are currently only supported at the topmost level of Regex

func Max

func Max(times int, sub ...*syntax.Regexp) *syntax.Regexp

func Maybe

func Maybe(sub ...interface{}) *syntax.Regexp

func Min

func Min(times int, sub ...*syntax.Regexp) *syntax.Regexp

func Range

func Range(rng ...rune) *syntax.Regexp

func Regex

func Regex(subs ...*syntax.Regexp) *regexp.Regexp

func Repeat

func Repeat(times int, sub ...*syntax.Regexp) *syntax.Regexp

func Sequence

func Sequence(subs ...*syntax.Regexp) *syntax.Regexp

func Then

func Then(match string) *syntax.Regexp

Types

This section is empty.

Jump to

Keyboard shortcuts

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