Skip to content

"\r?" regex doesn't seem to work properly  #718

Open
@MarsupialFan

Description

@MarsupialFan

Description

Regex("\r?\n") and other variants don't match "\r\n":

    print(try #/\r?\n/#.firstMatch(in: "\r\n") == nil ? "not matched" : "matched")  // prints "not matched"

See more "not matching" variants, and some related "matching" ones, in the reproduction section below.

Reproduction

        print(try #/\r?\n/#.firstMatch(in: "\n") == nil ? "not matched" : "matched")  // prints "matched"
        print(try #/\r?\n/#.firstMatch(in: "\n") == nil ? "not matched" : "matched")  // prints "matched"
        print(try #/(\r)?\n/#.firstMatch(in: "\n") == nil ? "not matched" : "matched")  // prints "matched"
        print(try #/\r{0,1}\n/#.firstMatch(in: "\n") == nil ? "not matched" : "matched")  // prints "matched"
        print(try #/(\r){0,1}\n/#.firstMatch(in: "\n") == nil ? "not matched" : "matched")  // prints "matched"
        print(try #/\r?\n/#.firstMatch(in: "\r\n") == nil ? "not matched" : "matched")  // prints "not matched"
        print(try #/(\r)?\n/#.firstMatch(in: "\r\n") == nil ? "not matched" : "matched")  // prints "not matched"
        print(try #/\r{0,1}\n/#.firstMatch(in: "\r\n") == nil ? "not matched" : "matched")  // prints "not matched"
        print(try #/(\r){0,1}\n/#.firstMatch(in: "\r\n") == nil ? "not matched" : "matched")  // prints "not matched"
        print(try #/\r\n/#.firstMatch(in: "\r\n") == nil ? "not matched" : "matched")  // prints "matched"
        print(try Regex("\r?\n").firstMatch(in: "\n") == nil ? "not matched" : "matched")  // prints "matched"
        print(try Regex("\r?\n").firstMatch(in: "\r\n") == nil ? "not matched" : "matched")  // prints "not matched"
        print(try Regex("\r\n").firstMatch(in: "\r\n") == nil ? "not matched" : "matched")  // prints "matched"

Expected behavior

I expect all the "not matched" lines above to print "matched"

Environment

swift-driver version: 1.87.3 Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5)
Target: arm64-apple-macosx14.0

Additional information

Xcode 15.2 (15C500b)
macOS Sonoma 14.2.1
Mac mini M2 2023

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions