英文:
Getting Parse Tree of a Regex in Go
问题
我尝试使用regex.syntax
模块来访问解析后的正则表达式的各个标记,但没有成功:我只能输出正则表达式的简化/优化版本。
代码:
package main
import (
"fmt"
"regexp/syntax"
)
func main() {
p, e := syntax.Parse(`[0120-2]@[ab][0-9]`, 'i')
fmt.Println(p)
fmt.Println(e)
}
输出:
[0-2](?i:@)[A-Ba-b][0-9]
<nil>
有人可以给我一个简单的例子,展示如何遍历并输出它的解析树吗?
英文:
I tried using the regex.syntax
module to access the individual tokens of a parsed regular expression without success: the only thing I'm able to output is a simplified/optimized version of the regex.
Code:
package main
import (
"fmt"
"regexp/syntax"
)
func main() {
p, e := syntax.Parse(`[0120-2]@[ab][0-9]`, 'i')
fmt.Println(p)
fmt.Println(e)
}
Output:
[0-2](?i:@)[A-Ba-b][0-9]
<nil>
Can someone give me a simple example of how to traverse and output its parse tree?
答案1
得分: 4
你调用的Parse
函数是正确的。当你调用fmt.Println(p)
时,解析树被转换为字符串,这就是为什么你看到的输出只是一个等价的正则表达式。
Parse
的返回值是一个指向syntax.Regexp
结构体的指针。要遍历返回的解析树,你可以查看返回结构体的Sub
字段,该字段列出了所有的子表达式(一个指向syntax.Regexp
结构体的切片)。例如:
func printSummary(r *syntax.Regexp) {
fmt.Printf("%v has %d sub expressions\n", r, len(r.Sub))
for i, s := range r.Sub {
fmt.Printf("Child %d:\n", i)
printSummary(s)
}
}
请参阅syntax包的参考文档了解更多值得检查的字段:Op
和Rune
是主要的字段之一。
英文:
The Parse
function you're calling is right. When you call fmt.Println(p)
, the parse tree is being converted to a string, which is why the output you're seeing is just an equivalent regexp.
The return value of Parse
is a pointer to a syntax.Regexp
struct. To traverse the returned parse tree you want to look at the Sub
field of the returned struct which lists all the subexpressions (a slice of pointers to syntax.Regexp
structs). For example:
func printSummary(r *syntax.Regexp) {
fmt.Printf("%v has %d sub expressions\n", r, len(r.Sub))
for i, s := range r.Sub {
fmt.Printf("Child %d:\n", i)
printSummary(s)
}
}
See the syntax package reference for more fields worth inspecting: Op
and Rune
are major ones.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论