regarding regex package

115 views
Skip to first unread message

Durga Someswararao G

unread,
Sep 14, 2019, 3:39:48 AM9/14/19
to golang-nuts
Hi,

I tried to execute one regular expression with regex package. When I try to compile I am getting unsupported Perl syntax. After reading regex syntax conclusion is golang not supporting some combination regex patterns like positive lookahed,negative lookahed etc. Is there any alternative or anyway to handle this in golang.


Thanks,
Durga SomeswaraRao G.

Ian Lance Taylor

unread,
Sep 14, 2019, 12:17:06 PM9/14/19
to Durga Someswararao G, golang-nuts
Not supporting all of Perl's regexp features is an intentional
decision. See the discussion at
https://swtch.com/~rsc/regexp/regexp1.html.

When I search for "golang pcre" I see several Go packages that support
Perl regular expressions, mostly by calling out to a PCRE library
written in C.

Ian

Doug Clark

unread,
Sep 14, 2019, 7:55:47 PM9/14/19
to golang-nuts
There’s a pure Go (no cgo needed) regexp engine that supports lookahead and lookbehind here: https://github.com/dlclark/regexp2

However, and I say this as someone who maintains that regex library, use this type of regex with caution. I would highly recommend changing your code to use the native, RE2-based engine if you can. Runaway regex’s on certain inputs is a risk with backtracking and is frequently a bad trade off in production.

If you need compatibility because you don’t control the patterns being used or are porting code with lots of existing patterns (which is why the library exists) then regexp2 is nice to have in your back pocket.

-Doug
Reply all
Reply to author
Forward
0 new messages