Skip to content

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

Closed

Description

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.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    bugIndicates an unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions