Giter VIP home page Giter VIP logo

goback's Introduction

goback

Build Status GoDoc

goback provides extended regexp syntax, such as Back reference.

The implementation does NOT guarantee linear processing time.

Syntax

See http://godoc.org/github.com/h2so5/goback/regexp/syntax

Examples

Back reference

package main

import (
	"fmt"
	"github.com/h2so5/goback/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

Lookbehind

re := regexp.MustCompile(`(?<=a[0-9]{3,5})a`)
fmt.Println(re.MatchString("a12a"))     // false
fmt.Println(re.MatchString("a12345a"))  // true

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.