Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RegEx support using RE2 #665

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Add RegEx support using RE2 #665

wants to merge 1 commit into from

Conversation

dcoles
Copy link
Contributor

@dcoles dcoles commented Jun 2, 2019

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.

@googlebot
Copy link

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).

📝 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.

@dcoles
Copy link
Contributor Author

dcoles commented Jun 2, 2019

Hey @googlebot, I signed it. 😄

@googlebot
Copy link

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@dcoles
Copy link
Contributor Author

dcoles commented Jun 2, 2019

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.

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.
@sparkprime
Copy link
Member

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/

@rbauduin
Copy link
Contributor

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

@nojvek
Copy link

nojvek commented Sep 10, 2020

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

@sbarzowski
Copy link
Collaborator

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.

@nojvek
Copy link

nojvek commented Sep 10, 2020

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.

@sparkprime
Copy link
Member

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)

@sbarzowski
Copy link
Collaborator

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.

@sparkprime
Copy link
Member

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.

@sbarzowski
Copy link
Collaborator

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...

@Duologic
Copy link
Contributor

I rebased this PR here: #1039

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

stdlib: add support regexp match and replace for strings
7 participants