本文整理汇总了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
}
示例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
}
示例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)
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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()
}
示例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
}
示例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
}
示例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)
}
示例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
}
示例15: newRegexpCache
func newRegexpCache(r *regexp.Regexp) *regexpCache {
s := r.String()
re, _ := syntax.Parse(s, syntax.Perl)
return ®expCache{
re: re,
min: minCap(re),
max: re.MaxCap(),
cache: make(map[string]*regexp.Regexp),
}
}