Skip to content

RegexBuilder: multiple Captures with transforms are clobbering each others' transformed values #83022

@seansfkelley

Description

@seansfkelley

Description

Later transforms from Captures seem to be clobbering the transformed output from earlier Captures in some cases.

Reproduction

A toy mixed-number regex:

import RegexBuilder

let mixedNumberRegex = Regex {
    // whole number
    Optionally {
        Capture {
            OneOrMore(.digit)
        } transform: { Int($0)! }
        OneOrMore { " " }
    }
    // numerator
    Capture {
        OneOrMore(.digit)
    } transform: { Int($0)! }
    "/"
    // denominator
    Capture {
        OneOrMore(.digit)
    } transform: { Int($0)! }
}

if let match = try? mixedNumberRegex.wholeMatch(in: "1 3/4") {
  print(match.1, match.2, match.3)
}

This prints Optional(1) 4 4.

Some further observations:

  • If the Int($0)! transforms are replaced by String($0), you correctly get Optional("1") 3 4.
  • If you remove the transforms entirely, the Substring matches are correct.
  • If you test with input string "3/4", you correctly get nil 3 4 -- no clobbering.

Expected behavior

It prints Optional(1) 3 4.

Environment

swift-driver version: 1.120.5 Apple Swift version 6.1.2 (swiftlang-6.1.2.1.2 clang-1700.0.13.5)
Target: arm64-apple-macosx15.0

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.triage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions