从正则表达式构造字符串

I've trying to find out a way to construct string data from a compiled *regexp.Regexp with given interface{} array. For example:

re := regexp.MustCompile(`(?P<word>\w+)\s*(?P<num>\d+)`)

I want to construct a string from the structure found in re by a string and a int data which may be received as interface{}.

Can't figure out how I can do that in Go. Please help me out.

Thanks in advance.

While technically not impossible this is a huge amount of work.

Go regexp package compiles regular expressions into bytecode programs that are then executed to search strings. De-compiling this bytecode back into a regular expression is not impossible but quite difficult and moreover you cannot be sure that the regexp expression you get is the exact same that was used originally... for example

/(?:a+)+/

and

/a+/

will be compiled to the same code for matching (expressions are "simplified" before being passed to the code generator).

Most probably you want to find another solution, like saving the original strings before compiling them into regular expression objects.

Such a library, often called Xeger, exists for many languages, including go. However, this one is called regen: It's a tool to generate random strings from Go/RE2 regular expressions.

Here's an example:

$ regen -n 3 '[a-z]{6,12}(\+[a-z]{6,12})?@[a-z]{6,16}(\.[a-z]{2,3}){1,2}'
iprbph+gqastu@regegzqa.msp
abxfcomj@uyzxrgj.kld.pp
vzqdrmiz@ewdhsdzshvvxjk.pi

Essentially, all regen does is parse the regular expressions it's given and iterate over the tree produced by regexp/syntax and attempt to generate strings based on the ops described by its results. This could probably be optimized further by compiling the resulting Regexp into a Prog, but I didn't feel like this was worthwhile when it's a very small tool.

Some additional information can be found at https://godoc.org/go.spiff.io/regen.