# README
regexp
from delete http://godoc.org/github.com/h2so5/goback/ golang regexp extended regexp syntax, such as Back reference. The implementation does NOT guarantee linear processing time.
Examples
Back reference
package main
import (
"fmt"
"github.com/dmskys/regexp"
)
func main() {
re := regexp.MustCompile(`^(\w)\w+\k{1}$`)
fmt.Println(re.MatchString("acca")) // true
fmt.Println(re.MatchString("accccab")) // false
fmt.Println(re.MatchString("AA")) // false
}
Possessive qualifiers
re := regexp.MustCompile(`^[0-9]++[0-9a]`)
fmt.Println(re.MatchString("1234a")) // true
fmt.Println(re.MatchString("1234")) // false
Atomic group
re := regexp.MustCompile(`^(?>[0-9]+)[0-9a]`)
fmt.Println(re.MatchString("1234a")) // true
fmt.Println(re.MatchString("1234")) // false
Comment
re := regexp.MustCompile(`(?#comment here)1234`)
fmt.Println(re.MatchString("1234")) // true
Lookahead
re := regexp.MustCompile(`a(?=[0-9]{3})1`)
fmt.Println(re.MatchString("a123")) // true
fmt.Println(re.MatchString("a12a")) // false
Free-Spacing mode
re := regexp.MustCompileFreeSpacing(`
[0-9]+ # one or more digits
[a-zA-Z]* # zero or more alphabets
\# # literal '#'
[ ] # literal ' '
`)
fmt.Println(re.MatchString("1234# ")) // true
fmt.Println(re.MatchString("12345abc ")) // false
Function
re := regexp.MustCompile(`(\d+)\+(\d+)=(?{add})`)
re.Funcs(syntax.FuncMap{
"add": func(ctx syntax.Context) interface{} {
lhs, err := strconv.Atoi(string(ctx.Data[ctx.Matches[1][0]:ctx.Matches[1][1]]))
if err != nil {
return -1
}
rhs, err := strconv.Atoi(string(ctx.Data[ctx.Matches[2][0]:ctx.Matches[2][1]]))
if err != nil {
return -1
}
answer := strconv.Itoa(lhs + rhs)
if bytes.HasPrefix(ctx.Data[ctx.Cursor:], []byte(answer)) {
return len(answer)
}
return -1
},
})
fmt.Println(re.MatchString("12+10=22")) // true
fmt.Println(re.MatchString("1+1=5")) // false
# Packages
Package syntax parses regular expressions into parse trees and compiles
parse trees into programs.
# Functions
Compile parses a regular expression and returns, if successful, a Regexp object that can be used to match against text.
CompileFreeSpacing parses a regular expression like Compile, but whitespace characters are ignored and # is parsed as the beggining of a line comment.
Match checks whether a textual regular expression matches a byte slice.
MatchString checks whether a textual regular expression matches a string.
MustCompile is like Compile but panics if the expression cannot be parsed.
MustCompileFreeSpacing parses a regular expression like MustCompile, but whitespace characters are ignored and # is parsed as the beggining of a line comment.
QuoteMeta returns a string that quotes all regular expression metacharacters inside the argument text; the returned string is a regular expression matching the literal text.
# Interfaces
Regexp is the representation of a compiled regular expression.