You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
With this PR, I'm adding a handful of methods to expose regular
expressions in jsonnet, through std.native()
I'm modeling them after [this open PR from
jsonnet](google/jsonnet#1039), which was ported
to jrsonnet.
For now, they are in std.native() as they are not part of the default
std package and use RE2 instead of the native regexp package (for
performance and compatibility reasons with a future go-jsonnet
implementation).
- regexFullMatch(pattern, str) -- Full match regex
- regexPartialMatch(pattern, str) -- Partial match regex
- regexReplace(str, pattern, to) -- Replace single occurance using regex
- regexGlobalReplace(str, pattern, to) -- Replace globally using regex
and the utility function:
- regexQuoteMeta(str) -- Escape regex metachararacters
Those functions return a object:
```
std.native("regexFullMatch")("h(?P<mid>.*)o", "hello")
{
"captures": [
"ell"
],
"string": "hello"
}
```
This PR does not add support for the "namedCaptures" return field due to
some complications with scalajs and scalanative. Those language both use
the JDK Pattern class (js being powered by ECMA regex and Native being
powered by RE2(!)), but JDK<20 Pattern class does not have a
straightforward way to list the names of groups without some additional
hacks. This will be dealt with in a follow up PR.
This PR also adds the ability to cache patterns, and refactors all users
of regexes to use it.
0 commit comments