diff --git a/Tests/RegexTests/MatchTests.swift b/Tests/RegexTests/MatchTests.swift index e0f391507..3c5ac96eb 100644 --- a/Tests/RegexTests/MatchTests.swift +++ b/Tests/RegexTests/MatchTests.swift @@ -1397,6 +1397,44 @@ extension RegexTests { XCTAssertEqual(allRanges.count, 5) } + func testSubstringAnchors() throws { + let string = "123abc456def789" + let trimmed = string.dropFirst(3).dropLast(3) // "abc456def" + let prefixLetters = try Regex(#"^[a-z]+"#, as: Substring.self) + let postfixLetters = try Regex(#"[a-z]+$"#, as: Substring.self) + + // start anchor (^) should match beginning of substring + XCTExpectFailure { + XCTAssertEqual(trimmed.firstMatch(of: prefixLetters)?.output, "abc") + } + XCTExpectFailure { + XCTAssertEqual(trimmed.replacing(prefixLetters, with: ""), "456def") + } + + // end anchor ($) should match end of substring + XCTExpectFailure { + XCTAssertEqual(trimmed.firstMatch(of: postfixLetters)?.output, "def") + } + XCTExpectFailure { + XCTAssertEqual(trimmed.replacing(postfixLetters, with: ""), "abc456") + } + + // start anchor (^) should _not_ match beginning of subrange + XCTAssertEqual( + string.replacing( + prefixLetters, + with: "", + subrange: trimmed.startIndex..