Skip to content

SubstitutionString can't contain escape sequences \n, \t, etc. during replace #27125

Closed

Description

While trying to create documentation for SubstitutionString (#26497), I found that replace using SubstitutionStrings doesn't work if the substition string has a newline or other escape sequence in it.

julia> msg = "#Hello# from Julia";

julia> re = r"#(.+)# from (?<name>\w+)";

julia> subst = s"FROM: \g<name>\n MESSAGE: \1"
s"FROM: \\g<name>\\n MESSAGE: \\1"

julia> replace(msg, re => subst)
ERROR: Bad replacement string: FROM: \g<name>\n MESSAGE: \1
Stacktrace:
 [1] error at .\error.jl:33 [inlined]
 [2] replace_err at .\regex.jl:241 [inlined]
 [3] _replace(::Base.GenericIOBuffer{Array{UInt8,1}}, ::Base.SubstitutionString{String}, ::String, ::UnitRange{Int64}, ::Regex) at .\regex.jl:302
 [4] #replace#350(::Int64, ::Function, ::String, ::Pair{Regex,Base.SubstitutionString{String}}) at .\strings\util.jl:427
 [5] replace(::String, ::Pair{Regex,Base.SubstitutionString{String}}) at .\strings\util.jl:415
 [6] top-level scope

julia> subst = s"FROM: \g<name>; MESSAGE: \1"
s"FROM: \\g<name>; MESSAGE: \\1"

julia> replace(msg, re => subst) #works fine when \n is removed
"FROM: Julia; MESSAGE: Hello"

Looking at the code, when the _replace function (in regex.jl) sees a backslash, it expects another backslash, a digit or a 'g', and throws an error with replace_err if it doesn't see one of those. Another consequence of this is that the replacement string can't contain things like Unicode codepoint sequences (\u0B85).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    HacktoberfestGood for Hacktoberfest participantsstrings"Strings!"

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions