Skip to content

Different replace behavior #76

Description

@kiliankoe

Hi,

I'm trying to remove the contents of a string matching the following regex: (\((?:[A-N]\d?(?:, )?)+\)).

The idea is to remove these allergen-notices from a string like this:

Frikadelle von Rind und Schwein (A, A1, C, I, J) mit Paprika-Zwiebelgemüse (J), dazu Spätzle (A, A1, C)

I tried the following:

let regex = Regex(#" (\((?:[A-N]\d?(?:, )?)+\))"#)
let title = oldTitle.replacingAll(matching: regex, with: "")

For some reason this leads to "Paprika-Zwiebelgemüse (J)" being turned into "Paprika-Zwiebelgemüs)" and not "Paprika-Zwiebelgemüse" as expected.

When I use the native String API (.replacingOccurrences(of: #" (\((?:[A-N]\d?(?:, )?)+\))"#, with: "", options: .regularExpression)), it works like it should.

Is this a bug in Regex or am I doing something wrong? This is on iOS in case that's relevant.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions