Match.jl: Getting hold of regex captures? (`\1` doesn't seem to work.)

ryofurue
Sat, 01 Mar 2025 15:48:39 +0000
General Usage

In general, is it possible to get hold of regex captures outside the RegexMatch object? In particular, can we get them in a pattern match of Match.jl

This is what I have found so far:

using Match

function matchreg(s)
  @match s begin
    r"abc(\d+)" => println(s"\1") # capture doesn't work
    r"xyz(\d+)" => println(replace(s, r"xyz(\d+)" => s"\1"))
  end
end

matchreg("abc01") # => "\1" . . . doesn't work
matchreg("xyz02") # => "02" . . . works

In the successful example above, you need to carry out the same regex match on both sides of =>.

Using explicit RegexMatch objects is more tedious and less readable, if you have to branch on what the string matches with, where Match.jl shines.

In Ruby and Perl, you have the global variables $1, $2, . . . , which are the most recent captures.

5 posts - 3 participants