Skip to content

matches(of:) and firstMatch(of:) behave differently when multiline is used with negative lookahead #613

Closed
@fwgreen

Description

@fwgreen
import Foundation

let regex = /(?m)^\b.*(^(?!SERIAL|TT).+)/

let string = """
CONT NEG TESTS - ALL PINS

SERIAL NUMBER: 15      MODULE: 0          Mon Jan  1 03:41:42 2007
TT   STMT PIN MEAS VALUE      FORCING         LESS THAN  GREATER THAN
---- ---- --- --------------- --------------- ---------- ------------
"""

for match in string.matches(of: regex) {
    print(match.output.1)
}

print("")

print(string.firstMatch(of: regex)?.output.1 ?? "")

Expected output:

CONT NEG TESTS - ALL PINS

CONT NEG TESTS - ALL PINS

Actual output:

CONT NEG TESTS - ALL PINS
---- ---- --- --------------- --------------- ---------- ------------

CONT NEG TESTS - ALL PINS

While removing multiline fixes it, this is only one capture group of a larger regex; and it works as expected, with multiline, in both Java (on my local machine) and the PCRE2 flavor on Regex101.com.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions