Skip to content

Add a "SubstitutionFunction" for replace #36293

@tlienart

Description

@tlienart

context: https://discourse.julialang.org/t/using-replace-with-a-function-of-the-match/41264 ; the suggestion below was made by @simeonschaub; in short the idea would be to allow:

foo(m::RegexMatch) = uppercase(m.captures[1])
rx = r"b([a-z])r"
replace("foobar", rx => SubstitutionFunction(foo))
# fooA 

i.e. introduce a new SubstitutionFunction type that would wrap a function which would get passed the regex match and allow for that regex match to be processed specifically before doing the replacement.

At the moment, in order to achieve this, one has to do a double match (at least as far as I'm aware):

foo(m::RegexMatch) = uppercase(m.captures[1])
rx = r"b([a-z])r"
replace("foobar", rx => s -> foo(match(rx, s)))

which seems a bit dumb.

I'm interested in trying a PR for this but would like to collect feedback first as to whether people think it's a good idea? (also advice as to where to put the code whether close to replace or close to SubstitutionString or somewhere else, would be welcome)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions