當前位置: 首頁>>代碼示例>>Golang>>正文


Golang syntax.Regexp類代碼示例

本文整理匯總了Golang中regexp/syntax.Regexp的典型用法代碼示例。如果您正苦於以下問題:Golang Regexp類的具體用法?Golang Regexp怎麽用?Golang Regexp使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Regexp類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: createRepeatingGenerator

// Returns a generator that will run the generator for r's sub-expression [min, max] times.
func createRepeatingGenerator(regexp *syntax.Regexp, genArgs *GeneratorArgs, min, max int) (*internalGenerator, error) {
	if err := enforceSingleSub(regexp); err != nil {
		return nil, err
	}

	generator, err := newGenerator(regexp.Sub[0], genArgs)
	if err != nil {
		return nil, generatorError(err, "failed to create generator for subexpression: /%s/", regexp)
	}

	if min == noBound {
		min = int(genArgs.MinUnboundedRepeatCount)
	}
	if max == noBound {
		max = int(genArgs.MaxUnboundedRepeatCount)
	}

	return &internalGenerator{regexp.String(), func() string {
		n := min + genArgs.rng.Intn(max-min+1)

		var result bytes.Buffer
		for i := 0; i < n; i++ {
			result.WriteString(generator.Generate())
		}
		return result.String()
	}}, nil
}
開發者ID:zach-klippenstein,項目名稱:memebot,代碼行數:28,代碼來源:internal_generator.go

示例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))
}
開發者ID:zach-klippenstein,項目名稱:memebot,代碼行數:12,代碼來源:internal_generator.go

示例3: opConcat

func opConcat(regexp *syntax.Regexp, genArgs *GeneratorArgs) (*internalGenerator, error) {
	enforceOp(regexp, syntax.OpConcat)

	generators, err := newGenerators(regexp.Sub, genArgs)
	if err != nil {
		return nil, generatorError(err, "error creating generators for concat pattern /%s/", regexp)
	}

	return &internalGenerator{regexp.String(), func() string {
		return genArgs.Executor.Execute(generators)
	}}, nil
}
開發者ID:Psiphon-Inc,項目名稱:goregen,代碼行數:12,代碼來源:internal_generator.go

示例4: singleLiteralToCharClass

func singleLiteralToCharClass(rx *syntax.Regexp) {
	if rx.Op == syntax.OpLiteral && len(rx.Rune) == 1 {
		char := rx.Rune[0]
		if rx.Flags&syntax.FoldCase != 0 && unicode.ToLower(char) != unicode.ToUpper(char) {
			l, h := unicode.ToLower(char), unicode.ToUpper(char)
			rx.Rune = []rune{h, h, l, l}
			rx.Rune0 = [...]rune{h, h}
		} else {
			rx.Rune = []rune{char, char}
			rx.Rune0 = [...]rune{char, char}
		}
		rx.Op = syntax.OpCharClass
	}
}
開發者ID:h12w,項目名稱:gombi,代碼行數:14,代碼來源:regexp.go

示例5: opConcat

func opConcat(regexp *syntax.Regexp, genArgs *GeneratorArgs) (*internalGenerator, error) {
	enforceOp(regexp, syntax.OpConcat)

	generators, err := newGenerators(regexp.Sub, genArgs)
	if err != nil {
		return nil, generatorError(err, "error creating generators for concat pattern /%s/", regexp)
	}

	return &internalGenerator{regexp.String(), func() string {
		var result bytes.Buffer
		for _, generator := range generators {
			result.WriteString(generator.Generate())
		}
		return result.String()
	}}, nil
}
開發者ID:zach-klippenstein,項目名稱:memebot,代碼行數:16,代碼來源:internal_generator.go

示例6: opAlternate

func opAlternate(regexp *syntax.Regexp, genArgs *GeneratorArgs) (*internalGenerator, error) {
	enforceOp(regexp, syntax.OpAlternate)

	generators, err := newGenerators(regexp.Sub, genArgs)
	if err != nil {
		return nil, generatorError(err, "error creating generators for alternate pattern /%s/", regexp)
	}

	numGens := len(generators)

	return &internalGenerator{regexp.String(), func() string {
		i := genArgs.rng.Intn(numGens)
		generator := generators[i]
		return generator.Generate()
	}}, nil
}
開發者ID:zach-klippenstein,項目名稱:memebot,代碼行數:16,代碼來源:internal_generator.go

示例7: createRepeatingGenerator

// Returns a generator that will run the generator for r's sub-expression [min, max] times.
func createRepeatingGenerator(regexp *syntax.Regexp, genArgs *GeneratorArgs, min int, max int) (*internalGenerator, error) {
	if err := enforceSingleSub(regexp); err != nil {
		return nil, err
	}

	generator, err := newGenerator(regexp.Sub[0], genArgs)
	if err != nil {
		return nil, generatorError(err, "failed to create generator for subexpression: /%s/", regexp)
	}

	if max < 0 {
		max = maxUpperBound
	}

	return &internalGenerator{regexp.String(), func() string {
		n := min + genArgs.rng.Intn(max-min+1)
		return executeGeneratorRepeatedly(genArgs.Executor, generator, n)
	}}, nil
}
開發者ID:Psiphon-Inc,項目名稱:goregen,代碼行數:20,代碼來源:internal_generator.go

示例8: opCapture

func opCapture(regexp *syntax.Regexp, args *GeneratorArgs) (*internalGenerator, error) {
	enforceOp(regexp, syntax.OpCapture)

	if err := enforceSingleSub(regexp); err != nil {
		return nil, err
	}

	groupRegexp := regexp.Sub[0]
	generator, err := newGenerator(groupRegexp, args)
	if err != nil {
		return nil, err
	}

	// Group indices are 0-based, but index 0 is the whole expression.
	index := regexp.Cap - 1

	return &internalGenerator{regexp.String(), func() string {
		return args.CaptureGroupHandler(index, regexp.Name, groupRegexp, generator, args)
	}}, nil
}
開發者ID:zach-klippenstein,項目名稱:memebot,代碼行數:20,代碼來源:internal_generator.go

示例9: 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
}
開發者ID:alexflint,項目名稱:go-restructure,代碼行數:31,代碼來源:regexp.go

示例10: 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)
		}
	}
}
開發者ID:RajibTheKing,項目名稱:gcc,代碼行數:23,代碼來源:onepass_test.go

示例11: transform

// transform replaces each node in a regex AST with the return value of the given function
// it processes the children of a node before the node itself
func transform(expr *syntax.Regexp, f transformer) (*syntax.Regexp, error) {
	var newchildren []*syntax.Regexp
	for _, child := range expr.Sub {
		newchild, err := transform(child, f)
		if err != nil {
			return nil, err
		}
		replacements, err := f(newchild)
		if err != nil {
			return nil, err
		}
		newchildren = append(newchildren, replacements...)
	}
	expr.Sub = newchildren
	return expr, nil
}
開發者ID:alexflint,項目名稱:go-restructure,代碼行數:18,代碼來源:transform.go

示例12: CompileSyntax

// CompileSyntax is like Compile but takes a syntax tree as input.
func CompileSyntax(ast *syntax.Regexp) (*Regexp, error) {
	return compileSyntax(ast, ast.String(), true)
}
開發者ID:alexflint,項目名稱:go-restructure,代碼行數:4,代碼來源:regexp.go

示例13: noop

// Generator that does nothing.
func noop(regexp *syntax.Regexp, args *GeneratorArgs) (*internalGenerator, error) {
	return &internalGenerator{regexp.String(), func() string {
		return ""
	}}, nil
}
開發者ID:zach-klippenstein,項目名稱:memebot,代碼行數:6,代碼來源:internal_generator.go

示例14: opEmptyMatch

func opEmptyMatch(regexp *syntax.Regexp, args *GeneratorArgs) (*internalGenerator, error) {
	enforceOp(regexp, syntax.OpEmptyMatch)
	return &internalGenerator{regexp.String(), func() string {
		return ""
	}}, nil
}
開發者ID:zach-klippenstein,項目名稱:memebot,代碼行數:6,代碼來源:internal_generator.go

示例15: opCharClass

// Handles syntax.ClassNL because the parser uses that flag to generate character
// classes that respect it.
func opCharClass(regexp *syntax.Regexp, args *GeneratorArgs) (*internalGenerator, error) {
	enforceOp(regexp, syntax.OpCharClass)
	charClass := parseCharClass(regexp.Rune)
	return createCharClassGenerator(regexp.String(), charClass, args)
}
開發者ID:zach-klippenstein,項目名稱:memebot,代碼行數:7,代碼來源:internal_generator.go


注:本文中的regexp/syntax.Regexp類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。