jsonnet icon indicating copy to clipboard operation
jsonnet copied to clipboard

Add RegEx support using RE2

Open dcoles opened this issue 5 years ago • 14 comments

Introduces 5 new built-in methods to the stdlib:

  • regexFullMatch(pattern, str) -- Full match regex
  • regexPartialMatch(pattern, str) -- Partial match regex
  • regexQuoteMeta(str) -- Escape regex metachararacters
  • regexReplace(str, pattern, to) -- Replace single occurance using regex
  • regexGlobalReplace(str, pattern, to) -- Replace globally using regex

Since both regexFullMatch and regexPartialMatch can perform captures these functions return a "match" object upon match or null otherwise. For example:

$ ./jsonnet -e 'std.regexFullMatch("h(?P<mid>.*)o", "hello")'
{
   "captures": [
      "ell"
   ],
   "namedCaptures": {
      "mid": "ell"
   },
   "string": "hello"
}

Introduces a dependency on RE2 2019-06-01. Builds tested using make, CMake and Bazel on Ubuntu 18.04.

Fixes #107.

dcoles avatar Jun 02 '19 03:06 dcoles

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

:memo: Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here (e.g. I signed it!) and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

googlebot avatar Jun 02 '19 03:06 googlebot

Hey @googlebot, I signed it. 😄

dcoles avatar Jun 02 '19 03:06 dcoles

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

googlebot avatar Jun 02 '19 03:06 googlebot

Linux and OSX build fails on make due to a missing dependency on RE2. For Linux I can update .travis.yml to pull in libre2-dev, but I'm not sure the right approach for OSX.

Bazel and CMake builds should be unaffected since they pull in the RE2 source via Git.

dcoles avatar Jun 02 '19 03:06 dcoles

It looks like on OSX you can do:

addons:
  homebrew:
    packages:
    - re2

Judging by https://docs.travis-ci.com/user/installing-dependencies/#installing-packages-on-macos and http://macappstore.org/re2/

sparkprime avatar Jul 31 '19 11:07 sparkprime

I could really use this, is it still planned to include this?

rbauduin avatar Oct 31 '19 07:10 rbauduin

Will there be any progress on landing regex support for jsonnet? Almost been an year. Just wondering what the status of this PR is.

nojvek avatar Sep 10 '20 00:09 nojvek

We're happy to go forward if the PR author (or someone else) is willing to pick this up again and fix the build issues.

sbarzowski avatar Sep 10 '20 16:09 sbarzowski

Thanks for quick response @sbarzowski.

I see there are some merge conflicts and travis is failing with The command "sudo -E apt-get -yq --no-install-suggests --no-install-recommends $(travis_apt_get_options) install g++-4.9 libre2-dev" failed and exited with 100 during . I'll wait for a couple of days if @dcoles is going to pick it up again, otherwise I'll try to pick this up and give it a shot next week.

nojvek avatar Sep 10 '20 18:09 nojvek

I assume that anyone picking this up would have to find a compatible solution on the Go side too? (I.e. the same regex syntax and semantics)

sparkprime avatar Oct 25 '22 10:10 sparkprime

Golang stdlib is "almost" re2.

My stance on this is that regexps are too useful to miss out on them completely, but fully portable, formally-defined solutions are not available, so I'd be willing to compromise and have best-effort compatibility.

sbarzowski avatar Nov 06 '22 21:11 sbarzowski

That may be necessary if there is no alternative. We would need to allow the jsonnet code to know what implementation it's running as so it can choose a regex for that implementation. I also wonder what the impact would be for the unofficial implementations in rust, scala, etc.

sparkprime avatar Nov 08 '22 13:11 sparkprime

I think, in practice what people almost always want are very simple regexps which look the same everywhere, so probably checking platform is not going to be necessary for what the people actually want...

sbarzowski avatar Nov 28 '22 22:11 sbarzowski

I rebased this PR here: https://github.com/google/jsonnet/pull/1039

Duologic avatar Nov 29 '22 19:11 Duologic