https://github.com/night-codes/goback
Yet another golang regexp
https://github.com/night-codes/goback
Last synced: 3 months ago
JSON representation
Yet another golang regexp
- Host: GitHub
- URL: https://github.com/night-codes/goback
- Owner: night-codes
- License: mit
- Created: 2016-07-14T14:52:52.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2015-03-02T05:52:33.000Z (over 10 years ago)
- Last Synced: 2025-01-23T23:32:53.937Z (4 months ago)
- Language: Go
- Size: 1.35 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
goback
======[](https://travis-ci.org/h2so5/goback)
[](http://godoc.org/github.com/h2so5/goback/regexp)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
```go
package mainimport (
"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
```go
re := regexp.MustCompile(`^[0-9]++[0-9a]`)
fmt.Println(re.MatchString("1234a")) // true
fmt.Println(re.MatchString("1234")) // false
```### Atomic group
```go
re := regexp.MustCompile(`^(?>[0-9]+)[0-9a]`)
fmt.Println(re.MatchString("1234a")) // true
fmt.Println(re.MatchString("1234")) // false
```### Comment
```go
re := regexp.MustCompile(`(?#comment here)1234`)
fmt.Println(re.MatchString("1234")) // true
```### Lookahead
```go
re := regexp.MustCompile(`a(?=[0-9]{3})1`)
fmt.Println(re.MatchString("a123")) // true
fmt.Println(re.MatchString("a12a")) // false
```### Lookbehind
```go
re := regexp.MustCompile(`(?<=a[0-9]{3,5})a`)
fmt.Println(re.MatchString("a12a")) // false
fmt.Println(re.MatchString("a12345a")) // true
```### Free-Spacing mode
```go
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
```go
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
```