swiftlang / swiftlang/swift-experimental-string-processing

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

Open
#718 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Swift
Stars
308
Forks
52
Avg merge
12h 45m
Merged PRs (30d)
2

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproduction cases in the issue, comparing regex literals and Regex string patterns against "\r\n". Trace the matching behavior for optional carriage returns and verify the fix by making all listed "not matched" cases report a match while preserving the existing matching cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.