Expand/Shrink

gmatch

Definition: include builtins\regex.e

string res = gmatch(sequence re, string target, res)
Description: Draft - this is a very simple routine that took very little time or effort (or thought) to write, and may change in later releases.

Applies the regular expression re (in string or pre-compiled format) to target and returns res with any substitutions for \1 to \9, or -1 if no match could be found.

re: a regular expression such as "a(b*)" or the result of applying regex_compile() to such a string.
target: a string to be matched against the regular expression.
res: a string containing \1 to \9 that should be replaced with the results of any match.

Returns: an updated res or -1 if no match could be found.
Aside: probably better to return target rather than -1, but improvements really should be driven by those using this in anger, which simply ain’t me.
To achieve what Java manages with {`_(.+)_`,`<em>$1</em>`} this (currently) needs {`(.*)__(.+)__(.*)`,`\1<em>\2</em>\3`}, but I’m not dead set on that, specifically the extra outer groups, nor would I be against additional control flags or more than two of these little helper functions.
pwa/p2js: Supported.
Example:
?gmatch(`(\w+) changed to (\w+)`,"red changed to green",`\2 <- \1`) -- yields "green <- red"
See Also: gsub
Expand/Shrink