Skip to content

kshdb/regex_go

Repository files navigation

regex_go

由于golang标准库regexp不支持per语法,由于之前又搞过多年c#,所以比两者整理了一份 golang与Per5和.net兼容的正则处理包,如果对您有用请赏个star!

比较regexp和regex_go

Category regexp regex_go
Catastrophic backtracking possible no, constant execution time guarantees yes, if your pattern is at risk you can use the re.MatchTimeout field
Python-style capture groups (?Pre) yes no (yes in RE2 compat mode)
.NET-style capture groups (?re) or (?'name're) no yes
comments (?#comment) no yes
branch numbering reset (?|a|b) no no
possessive match (?>re) no yes
positive lookahead (?=re) no yes
negative lookahead (?!re) no yes
positive lookbehind (?<=re) no yes
negative lookbehind (?<!re) no yes
back reference \1 no yes
named back reference \k'name' no yes
named ascii character class [[:foo:]] yes no (yes in RE2 compat mode)
conditionals (?(expr)yes|no) no yes

使用例子:

package main

import (
	"fmt"
	regex "github.com/kshdb/regex_go"
)

func main() {
	//fmt.Println("测试")
	_str := `[{"AAA":"1111","BBB":"222","CCC":""333 444"","DDD":"555"},{"AAA":"1111","BBB":"222","CCC":""333 444"","DDD":"555"},{"AAA":"1111","BBB":"222","CCC":""333 444"","DDD":"555"},{"AAA":"1111","BBB":""333 444"","CCC":"555","DDD":""}]`
	reg := regex.MustCompile(`\"\"`, regex.RE2)
	m, _err := reg.Replace(_str, `"`, 0, -1)
	fmt.Println("执行的结果是", m, _err)

	_srt1 := `
hooudcnwtn.com 
hooeeoooeeeeeen.com 
www.oo234r44.ss.com 
hoo666666.com
	`
	reg1 := regex.MustCompile(`(?<=o{2})([2-6a-z\.]+)(?=\.)`, regex.RE2)
	//sss, _ := reg1.FindStringMatch(_srt1) //获取单个
	sss := regexp2FindAllString(reg1, _srt1) //获取批量
	fmt.Println("执行的结果是", sss)

}
/*
获取批量
*/
func regexp2FindAllString(re *regex.Regexp, s string) []string {
	var matches []string
	m, _ := re.FindStringMatch(s)
	for m != nil {
		matches = append(matches, m.String())
		m, _ = re.FindNextMatch(m)
	}
	return matches
}

About

golang与Per5和.net兼容的正则处理包

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages