当前位置: 首页>>代码示例>>Golang>>正文


Golang syntax.Parse函数代码示例

本文整理汇总了Golang中regexp/syntax.Parse函数的典型用法代码示例。如果您正苦于以下问题:Golang Parse函数的具体用法?Golang Parse怎么用?Golang Parse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Parse函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: parseStr

func parseStr(p string) *syntax.Regexp {
	r, err := syntax.Parse(p, syntax.Literal)
	if err != nil {
		panic(err)
	}
	return r
}
开发者ID:h12w,项目名称:gombi,代码行数:7,代码来源:regexp.go

示例2: compile

func compile(expr string, mode syntax.Flags, longest bool) (*Regexp, error) {
	re, err := syntax.Parse(expr, mode)
	if err != nil {
		return nil, err
	}
	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,
		numSubexp:   maxCap,
		subexpNames: capNames,
		cond:        prog.StartCond(),
		longest:     longest,
	}
	regexp.prefix, regexp.prefixComplete = prog.Prefix()
	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
}
开发者ID:ds2dev,项目名称:gcc,代码行数:30,代码来源:regexp.go

示例3: compile

func compile(expr string, mode syntax.Flags, longest bool) (*Regexp, error) {
	re, err := syntax.Parse(expr, mode)
	if err != nil {
		return nil, err
	}
	return compileSyntax(re, expr, longest)
}
开发者ID:alexflint,项目名称:go-restructure,代码行数:7,代码来源:regexp.go

示例4: NewGenerator

// NewGenerator creates a generator that returns random strings that match the regular expression in pattern.
// If args is nil, default values are used.
func NewGenerator(pattern string, args *GeneratorArgs) (generator Generator, err error) {
	if nil == args {
		args = &GeneratorArgs{}
	}

	var seed int64
	if nil == args.RngSource {
		seed = rand.Int63()
	} else {
		seed = args.RngSource.Int63()
	}
	rngSource := xorShift64Source(seed)
	args.rng = rand.New(&rngSource)

	// unicode groups only allowed with Perl
	if (args.Flags&syntax.UnicodeGroups) == syntax.UnicodeGroups && (args.Flags&syntax.Perl) != syntax.Perl {
		return nil, generatorError(nil, "UnicodeGroups not supported")
	}

	var regexp *syntax.Regexp
	regexp, err = syntax.Parse(pattern, args.Flags)
	if err != nil {
		return
	}

	var gen *internalGenerator
	gen, err = newGenerator(regexp, args)
	if err != nil {
		return
	}

	return gen, nil
}
开发者ID:zach-klippenstein,项目名称:goregen-demo,代码行数:35,代码来源:regen.go

示例5: terminal

func (b *builder) terminal(f reflect.StructField, fullName string) (*Field, *syntax.Regexp, error) {
	pattern := string(f.Tag)
	if pattern == "" {
		return nil, nil, nil
	}

	// TODO: check for sub-captures within expr and remove them
	expr, err := syntax.Parse(pattern, b.opts.SyntaxFlags)
	if err != nil {
		return nil, nil, fmt.Errorf(`%s: %v (pattern was "%s")`, fullName, err, f.Tag)
	}

	captureIndex := -1
	if isExported(f) {
		captureIndex = b.nextCaptureIndex()
		expr = &syntax.Regexp{
			Op:   syntax.OpCapture,
			Sub:  []*syntax.Regexp{expr},
			Name: f.Name,
			Cap:  captureIndex,
		}
	}
	field := &Field{
		index:   f.Index,
		capture: captureIndex,
	}

	return field, expr, nil
}
开发者ID:simple555a,项目名称:go-restructure,代码行数:29,代码来源:builder.go

示例6: NewGenerator

// NewGenerator creates a generator that returns random strings that match the regular expression in pattern.
// If args is nil, default values are used.
func NewGenerator(pattern string, inputArgs *GeneratorArgs) (generator Generator, err error) {
	args := GeneratorArgs{}

	// Copy inputArgs so the caller can't change them.
	if inputArgs != nil {
		args = *inputArgs
	}
	if err = args.initialize(); err != nil {
		return nil, err
	}

	var regexp *syntax.Regexp
	regexp, err = syntax.Parse(pattern, args.Flags)
	if err != nil {
		return
	}

	var gen *internalGenerator
	gen, err = newGenerator(regexp, &args)
	if err != nil {
		return
	}

	return gen, nil
}
开发者ID:zach-klippenstein,项目名称:memebot,代码行数:27,代码来源:regen.go

示例7: parsePat

func parsePat(p string) *syntax.Regexp {
	r, err := syntax.Parse(p, syntax.ClassNL|syntax.DotNL|syntax.MatchNL|syntax.PerlX|syntax.UnicodeGroups)
	if err != nil {
		panic(err)
	}
	return r
}
开发者ID:h12w,项目名称:gombi,代码行数:7,代码来源:regexp.go

示例8: regexStrings

// regexStrings returns a set of strings such that any string that matches re must
// contain at least one of the strings in the set. If no such set can be found,
// regexStrings returns an empty set.
func regexStrings(re string) (stringSet, error) {
	parsed, err := syntax.Parse(re, syntax.Perl)
	if err != nil {
		return nil, err
	}
	info := analyze(parsed)
	return info.bestSet(), nil
}
开发者ID:ranivishnu,项目名称:redwood,代码行数:11,代码来源:restring.go

示例9: New

func New(pattern string) (*Node, error) {
	r, err := syntax.Parse(pattern, syntax.Perl)
	if err != nil {
		return nil, err
	}

	return NewFromRegexp(r.Simplify()), nil
}
开发者ID:postfix,项目名称:re2dfa,代码行数:8,代码来源:nfa.go

示例10: sketchOnRegex

/*
I'm sorry, dear reader. I really am.

The problem here is to take an arbitrary regular expression and:
1. return a regular expression that is just like it, but left-anchored,
   preferring to return the original if possible.
2. determine a string literal prefix that all matches of this regular expression
   have, much like regexp.Regexp.Prefix(). Unfortunately, Prefix() does not work
   in the presence of anchors, so we need to write it ourselves.

What this actually means is that we need to sketch on the internals of the
standard regexp library to forcefully extract the information we want.

Unfortunately, regexp.Regexp hides a lot of its state, so our abstraction is
going to be pretty leaky. The biggest leak is that we blindly assume that all
regular expressions are perl-style, not POSIX. This is probably Mostly True, and
I think most users of the library probably won't be able to notice.
*/
func sketchOnRegex(re *regexp.Regexp) (*regexp.Regexp, string) {
	rawRe := re.String()
	sRe, err := syntax.Parse(rawRe, syntax.Perl)
	if err != nil {
		log.Printf("WARN(web): unable to parse regexp %v as perl. "+
			"This route might behave unexpectedly.", re)
		return re, ""
	}
	sRe = sRe.Simplify()
	p, err := syntax.Compile(sRe)
	if err != nil {
		log.Printf("WARN(web): unable to compile regexp %v. This "+
			"route might behave unexpectedly.", re)
		return re, ""
	}
	if p.StartCond()&syntax.EmptyBeginText == 0 {
		// I hope doing this is always legal...
		newRe, err := regexp.Compile(`\A` + rawRe)
		if err != nil {
			log.Printf("WARN(web): unable to create a left-"+
				"anchored regexp from %v. This route might "+
				"behave unexpectedly", re)
			return re, ""
		}
		re = newRe
	}

	// Run the regular expression more or less by hand :(
	pc := uint32(p.Start)
	atStart := true
	i := &p.Inst[pc]
	var buf bytes.Buffer
Sadness:
	for {
		switch i.Op {
		case syntax.InstEmptyWidth:
			if !atStart {
				break Sadness
			}
		case syntax.InstCapture, syntax.InstNop:
			// nop!
		case syntax.InstRune, syntax.InstRune1, syntax.InstRuneAny,
			syntax.InstRuneAnyNotNL:

			atStart = false
			if len(i.Rune) != 1 ||
				syntax.Flags(i.Arg)&syntax.FoldCase != 0 {
				break Sadness
			}
			buf.WriteRune(i.Rune[0])
		default:
			break Sadness
		}
		pc = i.Out
		i = &p.Inst[pc]
	}
	return re, buf.String()
}
开发者ID:RuiAAPeres,项目名称:OctifyPush,代码行数:76,代码来源:pattern.go

示例11: terminal

func (b *builder) terminal(f reflect.StructField, fullName string) (*Field, *syntax.Regexp, error) {
	pattern, err := b.extractTag(f.Tag)
	if err != nil {
		return nil, nil, fmt.Errorf("%s: %v", fullName, err)
	}
	if pattern == "" {
		return nil, nil, nil
	}

	// Parse the pattern
	expr, err := syntax.Parse(pattern, b.opts.SyntaxFlags)
	if err != nil {
		return nil, nil, fmt.Errorf(`%s: %v (pattern was "%s")`, fullName, err, f.Tag)
	}

	// Remove capture nodes within the AST
	expr, err = transform(expr, removeCaptures)
	if err != nil {
		return nil, nil, fmt.Errorf(`failed to remove captures from "%s": %v`, pattern, err)
	}

	// Determine the kind
	t := f.Type
	if t.Kind() == reflect.Ptr {
		t = t.Elem()
	}
	var role Role
	switch t {
	case emptyType:
		role = EmptyRole
	case stringType:
		role = StringScalarRole
	case byteSliceType:
		role = ByteSliceScalarRole
	case submatchType:
		role = SubmatchScalarRole
	}

	captureIndex := -1
	if isExported(f) {
		captureIndex = b.nextCaptureIndex()
		expr = &syntax.Regexp{
			Op:   syntax.OpCapture,
			Sub:  []*syntax.Regexp{expr},
			Name: f.Name,
			Cap:  captureIndex,
		}
	}
	field := &Field{
		index:   f.Index,
		capture: captureIndex,
		role:    role,
	}

	return field, expr, nil
}
开发者ID:alexflint,项目名称:go-restructure,代码行数:56,代码来源:builder.go

示例12: isLiteralRegexp

// isLiteralRegexp checks regexp is a simple literal or not.
func isLiteralRegexp(expr string, flags syntax.Flags) bool {
	re, err := syntax.Parse(expr, flags)
	if err != nil {
		return false
	}
	if re.Op == syntax.OpLiteral && re.Flags&syntax.FoldCase == 0 {
		return true
	}
	return false
}
开发者ID:tSU-RooT,项目名称:jvgrep,代码行数:11,代码来源:jvgrep.go

示例13: RegexMatch

// RegexMatch generates matches for a given regular expression
// regexStr is supposed to conform to the perl regular expression syntax
func RegexMatch(regexStr string) gopter.Gen {
	regexSyntax, err1 := syntax.Parse(regexStr, syntax.Perl)
	regex, err2 := regexp.Compile(regexStr)
	if err1 != nil || err2 != nil {
		return Fail(reflect.TypeOf(""))
	}
	return regexMatchGen(regexSyntax.Simplify()).SuchThat(func(v interface{}) bool {
		return regex.MatchString(v.(string))
	}).WithShrinker(StringShrinker)
}
开发者ID:untoldwind,项目名称:gorrd,代码行数:12,代码来源:regex.go

示例14: parseOrPanic

// ParseOrPanic parses a regular expression into an AST.
// Panics on error.
func parseOrPanic(simplify bool, pattern string) *syntax.Regexp {
	regexp, err := syntax.Parse(pattern, 0)
	if err != nil {
		panic(err)
	}
	if simplify {
		regexp = regexp.Simplify()
	}
	return regexp
}
开发者ID:Psiphon-Inc,项目名称:goregen,代码行数:12,代码来源:regexp_format.go

示例15: newRegexpCache

func newRegexpCache(r *regexp.Regexp) *regexpCache {
	s := r.String()
	re, _ := syntax.Parse(s, syntax.Perl)
	return &regexpCache{
		re:    re,
		min:   minCap(re),
		max:   re.MaxCap(),
		cache: make(map[string]*regexp.Regexp),
	}
}
开发者ID:rainycape,项目名称:gondola,代码行数:10,代码来源:regexp.go


注:本文中的regexp/syntax.Parse函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。