itchyny / gojq

Pure Go implementation of jq
MIT License
3.3k stars 119 forks source link

error parsing regexp: invalid named capture #246

Closed anthonyfok closed 7 months ago

anthonyfok commented 7 months ago

Hello,

I was trying to use GitHub CLI (gh)and its --jq flag (hence gojq) to simplify the command calls a bit, i.e. to do away with the need to piping gh output to jq. However, I ran into error parsing regexp: invalid named capture sometimes. For example, the following works with piping to jq:

$ gh api repos/OpenDRR/earthquake-scenarios/contents/FINISHED \
     | jq -r '.[].name | scan(".*s_shakemap_.*(?<!MMI)\\.csv")'`
s_shakemap_ACM4p9_GeorgiaStraitFault_1.csv
s_shakemap_ACM4p9_VedderFault_196.csv
...
s_shakemap_SIM9p0_CascadiaInterfaceBestFault_11.csv

but failed with --jq flag:

$ gh api repos/OpenDRR/earthquake-scenarios/contents/FINISHED \
     --jq '.[].name | scan(".*s_shakemap_.*(?<!MMI)\\.csv")'
invalid regular expression ".*s_shakemap_.*(?P<!MMI)\\.csv": error parsing regexp: invalid named capture: `(?P<!MMI)\.csv`

and likewise when piping to the gojq:

$ gh api repos/OpenDRR/earthquake-scenarios/contents/FINISHED \
     | gojq -r '.[].name | scan(".*s_shakemap_.*(?<!MMI)\\.csv")'
gojq: invalid regular expression ".*s_shakemap_.*(?P<!MMI)\\.csv": error parsing regexp: invalid named capture: `(?P<!MMI)\.csv`

Tested with gojq v0.12.11, v0.12.13, and latest commit 2cc65b621eba3357cad1b2d6d93f53ede6c13e27 dated 2024-03-30 (post v0.12.14).

Many thanks!

Anthony

itchyny commented 7 months ago

This is not supported, because gojq is written in Go, and regular expression engine is RE2, which does not support lookbehind and lookahead. See WhyRE2, and its Syntax.

anthonyfok commented 7 months ago

I see! Thank you so much for your explanation!

Edit: For comparison, jq uses the Oniguruma (鬼車) regular expression library (https://github.com/kkos/oniguruma)