user1467267
user1467267

Reputation:

Negative Lookbehind failure

I have a little MySQL table inspector script in my Golang code. The last thing I wanted to do was to store any possible enum value when my application starts so I can read it out of the buffer instead of having to describe the tables constantly for API calls.

Now with enum's stuff like 'value1','value2','value\'s3 can occur, where simple RegEx like '(.*?)' doesn't work because of the escaped quotation-mark in value\'s3. So I wrote a negative look-behind to make it match only if it is a non-escaped quotation-mark. Not totally flawless but at least some extra checks and I manage the database structure by myself so it's something I can just keep a small note on.

To the point; the negative-lookbehind fails '(.*?)(?<!\\)':

panic: regexp: Compile(`'(.*?)(?<!\\)'`): error parsing regexp: invalid or unsupported Perl syntax: `(?<`

Code:

var enumMatchValues = regexp.MustCompile(`'(.*?)(?<!\\)'`)
values := enumMatchValues.FindAllStringSubmatch(theRawEnumValuesString, -1)

Are the lookaheads/lookbehinds officially not part of the core of the RegEx functionality or is this just something Golang hasn't implemented yet?

Would there be another workaround or extended RegEx package for Go to solve this?

Upvotes: 2

Views: 317

Answers (1)

hwnd
hwnd

Reputation: 70732

Lookarounds are not supported, you could use a possible workaround:

re := regexp.MustCompile(`'(.*?[^\\]|)'`)
values := re.FindAllStringSubmatch(theRawEnumValuesString, -1)

Upvotes: 2

Related Questions