本文整理汇总了Golang中regexp/syntax.Regexp.Simplify方法的典型用法代码示例。如果您正苦于以下问题:Golang Regexp.Simplify方法的具体用法?Golang Regexp.Simplify怎么用?Golang Regexp.Simplify使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类regexp/syntax.Regexp
的用法示例。
在下文中一共展示了Regexp.Simplify方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: compileSyntax
func compileSyntax(re *syntax.Regexp, expr string, longest bool) (*Regexp, error) {
maxCap := re.MaxCap()
capNames := re.CapNames()
re = re.Simplify()
prog, err := syntax.Compile(re)
if err != nil {
return nil, err
}
regexp := &Regexp{
expr: expr,
prog: prog,
onepass: compileOnePass(prog),
numSubexp: maxCap,
subexpNames: capNames,
cond: prog.StartCond(),
longest: longest,
}
if regexp.onepass == notOnePass {
regexp.prefix, regexp.prefixComplete = prog.Prefix()
} else {
regexp.prefix, regexp.prefixComplete, regexp.prefixEnd = onePassPrefix(prog)
}
if regexp.prefix != "" {
// TODO(rsc): Remove this allocation by adding
// IndexString to package bytes.
regexp.prefixBytes = []byte(regexp.prefix)
regexp.prefixRune, _ = utf8.DecodeRuneInString(regexp.prefix)
}
return regexp, nil
}
示例2: newGenerator
// Create a new generator for r.
func newGenerator(regexp *syntax.Regexp, args *GeneratorArgs) (generator *internalGenerator, err error) {
simplified := regexp.Simplify()
factory, ok := generatorFactories[simplified.Op]
if ok {
return factory(simplified, args)
}
return nil, fmt.Errorf("invalid generator pattern: /%s/ as /%s/\n%s",
regexp, simplified, inspectRegexpToString(simplified))
}
示例3: TestCompileOnePass
func TestCompileOnePass(t *testing.T) {
var (
p *syntax.Prog
re *syntax.Regexp
err error
)
for _, test := range onePassTests {
if re, err = syntax.Parse(test.re, syntax.Perl); err != nil {
t.Errorf("Parse(%q) got err:%s, want success", test.re, err)
continue
}
// needs to be done before compile...
re = re.Simplify()
if p, err = syntax.Compile(re); err != nil {
t.Errorf("Compile(%q) got err:%s, want success", test.re, err)
continue
}
onePass = compileOnePass(p)
if (onePass == notOnePass) != (test.onePass == notOnePass) {
t.Errorf("CompileOnePass(%q) got %v, expected %v", test.re, onePass, test.onePass)
}
}
}