我正在使用go中的一些正则表达式,它不是一个直接的过程,即需要时间来完成并理解我发现的项目并通过手册快速阅读;任何关于改进以下内容的意见都将受到赞赏,以加快这一进程。
// {aa,bb,cc,dd, etc.}, {a+,b+,c+}
regexp.MustCompile(`\B\{([\w-]+)(.*,)([\w-]+)(?:,[\w-]+=[\w-]+)*\}`)
// above captures {a+, b+, c}, but not {a+,b+,c+}
// {1-9}, {1-9,10,19,20-52}
regexp.MustCompile(`\B\{([\d]?)-([\d]?)(?:,[\d]?=[\d]?)*\}`)
// the first is fine, but no idea on how to do the latter, i.e. multiple ranges that might have free standing addons, tricky, maybe beyond a regexp
// {a-f}, {A-F}, {x-C}
regexp.MustCompile(`\B\{([a-zA-Z]?)-([a-zA-Z]?)(?:,[a-zA-Z]?=[a-zA-Z]?)*\}`)
我不确定我是否需要(?:部分,它是找到的东西,我只需要在文本中识别上面的单独的序列实例(逗号分隔,数字范围,字符范围){}解析。
答案 0 :(得分:0)
The problem is easier to tackle if you break the parsing down into steps. You could start with something like:
http://play.golang.org/p/ugqMmaeKEs
s := "{aa,bb,cc, dd}, {a+,\tb+,c+}, {1-9}, {1-9,10,19,20-52}, {a-f}, {A-F}, {x-C}, {}"
// finds the groups of characters captured in {}
matchGroups := regexp.MustCompile(`\{(.+?)\}`)
// splits each captured group of characters
splitParts := regexp.MustCompile(`\s*,\s*`)
parts := [][]string{}
for _, match := range matchGroups.FindAllStringSubmatch(s, -1) {
parts = append(parts, splitParts.Split(match[1], -1))
}
Now you have all the parts grouped into slices, and can parse the syntax of the individual pieces without having to match all combinations via a regex.