Skip to content
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

Replace with optional matching group fails when there is no match #31456

Closed
tlienart opened this issue Mar 23, 2019 · 4 comments
Closed

Replace with optional matching group fails when there is no match #31456

tlienart opened this issue Mar 23, 2019 · 4 comments
Labels
bug Indicates an unexpected problem or unintended behavior

Comments

@tlienart
Copy link
Contributor

This is a repost from my question on discourse but I'm reasonably sure it's a bug so opening here. Everything below is ran with the most recent 1.2 binary on mac FWIW.

What fails:

replace("The quick fox run quickly.", r"fox(es)?" => s"bus\1")

Stacktrace:

ERROR: PCRE error: requested value is not set
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] substring_length_bynumber at ./pcre.jl:155 [inlined]
 [3] _write_capture(::Base.GenericIOBuffer{Array{UInt8,1}}, ::Regex, ::Int64) at ./regex.jl:286
 [4] _replace(::Base.GenericIOBuffer{Array{UInt8,1}}, ::SubstitutionString{String}, ::String, ::UnitRange{Int64}, ::Regex) at ./regex.jl:320
 [5] #replace#327(::Int64, ::Function, ::String, ::Pair{Regex,SubstitutionString{String}}) at ./strings/util.jl:459
 [6] replace(::String, ::Pair{Regex,SubstitutionString{String}}) at ./strings/util.jl:447
 [7] top-level scope at none:0

Note this example is adapted from the doc of replace just changing foxes (in the doc) for fox. So in this case the optional matching group does not match which is what the error says about \1 not being set. The expected behaviour should be to just get "The quick bus run quickly." i.e. the \1 should be injected with an empty string, I think.

FWIW, using Python 3.7:

import re
re.sub(r"fox(es)?", r"bus\1", "The quick fox run quickly.")
# 'The quick bus run quickly.'
@KristofferC KristofferC added the bug Indicates an unexpected problem or unintended behavior label Mar 28, 2019
@musm
Copy link
Contributor

musm commented Jan 2, 2021

Upstream issue?

@frankwswang
Copy link

I think this bug still exists in Julia 1.6. It seems to be related to using \N (for catching the Nth capture group of the regular expression) in the SubstitutionString.

julia> replace("The quick fox run quickly.", r"fox(es)?" => s"bus")
"The quick bus run quickly."

julia> replace("The quick fox run quickly.", r"fox(es)?" => s"bus\1")
ERROR: PCRE error: requested value is not set
Stacktrace:
 [1] error(s::String)
   @ Base .\error.jl:33
 [2] substring_length_bynumber
   @ .\pcre.jl:204 [inlined]
 [3] _write_capture(io::IOBuffer, re::Base.RegexAndMatchData, group::Int64)
   @ Base .\regex.jl:498
 [4] _replace(io::IOBuffer, repl_s::SubstitutionString{String}, str::String, r::UnitRange{Int64}, re::Base.RegexAndMatchData)
   @ Base .\regex.jl:535
 [5] replace(str::String, pat_repl::Pair{Regex, SubstitutionString{String}}; count::Int64)
   @ Base .\strings\util.jl:542
 [6] replace(str::String, pat_repl::Pair{Regex, SubstitutionString{String}})
   @ Base .\strings\util.jl:525
 [7] top-level scope
   @ REPL[44]:1

@vtjnash
Copy link
Member

vtjnash commented Apr 7, 2021

The PR #31493 seems to have stalled

Though, in this example, I'm not sure why you wouldn't mark the capture as an optional match of a required group

julia> replace("The quick fox run quickly.", r"fox(es?)" => s"bus\1")
"The quick fox run quickly."

@tlienart
Copy link
Contributor Author

tlienart commented Apr 7, 2021

I'm happy to pick it up where it stalled, just wasn't sure whether it was appropriate, thanks for the feedback there

Re the example, it's a slight modification from the docs (see https://discourse.julialang.org/t/replace-with-non-matching-group/22187), fair enough there's a workaround but this should still be fixed I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

5 participants