1(import posix-regex (edward replace))23(define (test-re str pattern replacement . o)4 (let-values (((result modified)5 (regex-replace6 (make-regex pattern)7 (parse (parse-replace #\/) replacement)8 str9 (if (null? o) 0 (car o)))))10 (cons result modified)))1112;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;1314(test-group "backreferences"15 (test "single backreference"16 '("faz" . #t)17 (test-re "foo" "\\([a-z]\\)oo" "\\1az"))18 (test "multiple backreferences"19 '("bar foo" . #t)20 (test-re "foo bar" "\\([a-z]*\\) \\([a-z]*\\)" "\\2 \\1"))21 (test "unused backreferences"22 '("2" . #t)23 (test-re "123" "\\([0-9]\\)\\([0-9]\\)\\([0-9]\\)" "\\2"))24 (test "escape subexpression"25 '("bar" . #t)26 (test-re "\\(foo\\)" "\\\\(foo\\\\)" "bar")))2728(test-group "ampersand"29 (test "append text"30 '("foobar" . #t)31 (test-re "foo" "foo" "&bar"))32 (test "append in middle"33 '("foo123bar" . #t)34 (test-re "foo1bar" "1" "&23"))35 (test "escape ampersand"36 '("&bar" . #t)37 (test-re "foo" "foo" "\\&bar")))3839(test-group "replacement amount"40 (test "replace first match only"41 '("testbarfoo" . #t)42 (test-re "foobarfoo" "foo" "test" 1))43 (test "replace all matches"44 '("testbartest" . #t)45 (test-re "foobarfoo" "foo" "test" 0))46 (test "replace second match only"47 '("foobartest" . #t)48 (test-re "foobarfoo" "foo" "test" 2)))4950(test-group "miscellaneous"51 (test "input string with multibyte character"52 '("foo|bar" . #t)53 (test-re "fooλbar" "λ" "|"))54 (test "replacement with multibyte character"55 '("fooλbar" . #t)56 (test-re "foo|bar" "|" "λ"))57 (test "replace with newline"58 '("foo\nbar" . #t)59 (test-re "foo|bar" "|" "\\\n"))60 (test "non-participating submatch"61 '("baz" . #t)62 (test-re "foo baz" "foo \\(..*\\)* \\(..*\\)" "\\2\\1"))63 (test "empty submatch"64 '("matched: " . #t)65 (test-re "foo <> baz"66 "foo <\\(.*\\)> baz"67 "matched: \\1")))6869(test-group "modified"70 (test "no match"71 '("foo" . #f)72 (test-re "foo" "bar" "test"))7374 (test "replace second match"75 '(" foo foo bar " . #t)76 (test-re " foo foo foo " " foo " " bar " 2)))