Getting the regex syntax tree in Go

I tried using regex.syntaxmodule to freely access the individual tokens of a syntactic regular expression: the only thing I can output is a simplified / optimized version of the regular expression.

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 navigate and display its parse tree?

+4
source share
1 answer

The function Parseyou are calling is right. When you call fmt.Println(p), the parse tree is converted to a string, so the output you see is just the equivalent regular expression.

Parse syntax.Regexp. , Sub , ( syntax.Regexp structs). :

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)
    }
}

. : Op Rune .

+4

All Articles