I’d like to match a string to a regular expression and use captured substring elsewhere, for example, /foo([a-z]*)bar/
. AFAIU, there is currently no way to use capture groups in current regexp
package. Is my understanding correct? If yes, where do I submit feature requests?
Hello @abbradar,
You can just filter with regex. You don’t need to use the regexp package:
@abbradar you might also find the following useful:
Specifically:
Can you please provide a full example of what you’re trying to achieve so I can help you better?
I’d like to extract a particular substring from a name of systemd service. Currently I achieve this in a somewhat ugly way by doing two replacements:
|> filter(fn: (r) => r["name"] =~ /^my-service-([^.]*)\.service$/)
|> map(fn: (r) => {
region_ = regexp.replaceAllString(r: /^my-service-/, t: "", v: r["name"])
region = regexp.replaceAllString(r: /\.service$/, t: "", v: region_)
return { r with region: region }
})
It would be nice to have regular expressions with capture groups instead to achieve this. Say, regexp.newReplaceAllString(r: /^my-service-([^.]*)\.service$/, t: "\1", v: r["name"])
, that should just yield needed string. It can also be used for more complex operations, for example regexp.newReplaceAllString(r: /^my-service-([^-]*)-([^.]*)\.service$/, t: "\2 \1", v: r["name"])
, when used on string “my-service-foo-bar.service”, should yield “bar foo”.