4

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?

Alix Axel
  • 151,645
  • 95
  • 393
  • 500
  • Please post an example of the code you've tried. Otherwise, it sounds like you're looking for the `Parse` function "Parse parses a regular expression string s, controlled by the specified Flags, and returns a regular expression parse tree." http://golang.org/pkg/regexp/syntax/#Parse – Intermernet Dec 14 '13 at 09:24
  • @Intermernet: Yeah, I was on mobile, sorry for not posting that right from the start. I think I need the `Parse` function as well but I'm not sure why it's not working. – Alix Axel Dec 14 '13 at 09:44

1 Answers1

4

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.

djd
  • 4,988
  • 2
  • 25
  • 35
  • Totally off-topic, but you do think it would be easier to achieve what I am asking in [this question](http://stackoverflow.com/questions/20815278/porting-invregex-py-to-javascript-node-js) in Go? AFAIK, it doesn't have support for generators (at least not the usual kind). – Alix Axel Dec 28 '13 at 14:00
  • 1
    You can get something similar to generator using channels: http://play.golang.org/p/0JXsGZf2Kl implements very simple cartesian product for string iterables. – val Jan 02 '14 at 11:29