itchyny / gojq

Pure Go implementation of jq
MIT License
3.26k stars 118 forks source link

error parsing regexp that contains "?<=" #255

Closed YuvalOmerRep closed 3 months ago

YuvalOmerRep commented 3 months ago

Found a problem with gojq when calling match on a regex containing "?<=" example:

❯ gojq -n "@text \"projectP\" | match(\"(?<=project).\")"
gojq: invalid regular expression "(?P<=project).": error parsing regexp: invalid named capture: `(?P<=project).

❯ jq -n "@text \"projectP\" | match(\"(?<=project).\")"
{
  "offset": 7,
  "length": 1,
  "string": "P",
  "captures": []
}

This error seems to be caused by the following line in compileRegexp: re = strings.ReplaceAll(re, "(?<", "(?P<")

wader commented 3 months ago

Hello, gojq uses https://pkg.go.dev/regexp for regexp which does not support looka-head/behind, jq uses https://github.com/kkos/oniguruma which do support it.

itchyny commented 3 months ago

Right, this is due to the difference of ragex engine.

wader commented 3 months ago

@YuvalOmerRep FYI the differences are documented here https://github.com/itchyny/gojq?tab=readme-ov-file#difference-to-jq