Consider supporting regular expressions #23
Replies: 6 comments 6 replies
-
Tu use regular expressions in a
regexp = …
function-arg =/ regexp ExampleConsider the following JSON document {
"assets": [
{ "name": "first-name" },
{ "name": "last-name" },
{ "name": "other" }
]
} To select all
This would evaluate to:
|
Beta Was this translation helpful? Give feedback.
-
To support regular expressions to replace substrings, we would need a new function:
For instance, using the same example as the previous comment, the following expression:
would evaluate to:
|
Beta Was this translation helpful? Give feedback.
-
Finally, a function could return the named or numbered captures from a regex.
Example: The simplest usage could look like:
Which evaluates to a JSON object if the match is successful: { "0": "hello" } Adding capturing groups would be supported like so:
Would return: {
"0": "hello, world!",
"1": "hello"
} Likewise, this expression:
Would evaluate to: {
"0": "hello, world!",
"first": "hello",
"second": "world"
} This is currently not very useful, but in light of the probably upcoming function let, we could have expressions like so:
|
Beta Was this translation helpful? Give feedback.
-
I like the idea of regular expression support, but think that it could be quite burdensome for some implementations. How would we handle varying RE specs (POSIX vs PCRE vs RE2) that are available for each language? I think we should have an explicitly optional spec for the function interface to improve consistency between implementations, but leave it up to the implementers as to whether they want to implement it and what RE spec it would use. |
Beta Was this translation helpful? Give feedback.
-
Reiterating my view
Good point, but how about providing a regular expressions function among the Built-in Functions. The regular expressions will be Go specific, but that's much better than not having any regular expression support, or supporting them all, for the regular expression matching part has been fully implemented: just its exposed name need to be changed. |
Beta Was this translation helpful? Give feedback.
-
I felt this should be fall into custom function/filter instead Also there was also regex replace with custom function that would be great to supported |
Beta Was this translation helpful? Give feedback.
-
There seems to be some demand for supporting regular expressions.
This presents some challenges as there are many different dialects of regexp, although there is an effort around
specifying an interoperable regex format.
Also, regular expressions uses cases fall into three main categories:
filter-expressions
. It either matches, or it does not.So a JEP would need to come up with intuitive syntax for those use cases that would be relevant.
The interoperable regex format proposal referred to above only supports the first scenario. So maybe that’s the only scenario that requires extending the JMESPath syntax.
The last two scenarios may be supported using dedicated functions.
Beta Was this translation helpful? Give feedback.
All reactions