From 2b28ce5b0c18c4f2d8a901c34e3226ea35e2e9cc Mon Sep 17 00:00:00 2001 From: Kevin Ryde Date: Fri, 27 Aug 2004 01:00:38 +0000 Subject: [PATCH] (regexp-quote): [ and | must be quoted. Quote ( ) { + ? using char class [(] etc since \( in fact makes them become special in regexp/basic. --- ice-9/regex.scm | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/ice-9/regex.scm b/ice-9/regex.scm index d49a9b5f1..0f5016c3c 100644 --- a/ice-9/regex.scm +++ b/ice-9/regex.scm @@ -43,6 +43,11 @@ string-match regexp-substitute fold-matches list-matches regexp-substitute/global)) +;; References: +;; +;; POSIX spec: +;; http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap09.html + ;;; FIXME: ;;; It is not clear what should happen if a `match' function ;;; is passed a `match number' which is out of bounds for the @@ -78,6 +83,21 @@ (loop (+ 1 i))) (else #f))))) +;; * . \ ^ $ and [ are special in both regexp/basic and regexp/extended and +;; can be backslash escaped. +;; +;; ( ) + ? { } and | are special in regexp/extended so must be quoted. But +;; that can't be done with a backslash since in regexp/basic where they're +;; not special, adding a backslash makes them become special. Character +;; class forms [(] etc are used instead. +;; +;; ) is not special when not preceded by a (, and * and ? are not special at +;; the start of a string, but we quote all of these always, so the result +;; can be concatenated or merged into some larger regexp. +;; +;; ] is not special outside a [ ] character class, so doesn't need to be +;; quoted. +;; (define (regexp-quote string) (call-with-output-string (lambda (p) @@ -85,9 +105,15 @@ (and (< i (string-length string)) (begin (case (string-ref string i) - ((#\* #\. #\( #\) #\+ #\? #\\ #\^ #\$ #\{ #\}) - (write-char #\\ p))) - (write-char (string-ref string i) p) + ((#\* #\. #\\ #\^ #\$ #\[) + (write-char #\\ p) + (write-char (string-ref string i) p)) + ((#\( #\) #\+ #\? #\{ #\} #\|) + (write-char #\[ p) + (write-char (string-ref string i) p) + (write-char #\] p)) + (else + (write-char (string-ref string i) p))) (loop (1+ i)))))))) (define (match:start match . args)