-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a "SubstitutionFunction" for replace #36293
Comments
The function applied to the string mapping transformation is not a function substituted for some other. What is a more apt name? |
|
I'm also really interested in being able to do this, and I proposed a different solution in #24598 before I found this issue. This solution with the function wrapper would
Advantages of my solution:
My main argument would be that the previous API is good for simple one-liners, and a replace("The quick foxes run quickly.", r"fox(es)?" => s"bus\1")
# The quick buses run quickly.
replace("The quick foxes run quickly.", r"quick|slow" => uppercase)
# The QUICK foxes run QUICKly.
replace("The quick foxes run quickly.", r"fox(es)") do match
"bus" * uppercase(match[1])
end
# The quick busES run quickly. I'm completely happy with either solution, or even both, as long as I'm able to do this kind of thing. In any case, both are non-breaking, which I'm a big fan of in general. |
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:
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):
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 toSubstitutionString
or somewhere else, would be welcome)The text was updated successfully, but these errors were encountered: