diff --git a/lib/chibi/regexp.scm b/lib/chibi/regexp.scm index 0f9a655f..e1b892fa 100644 --- a/lib/chibi/regexp.scm +++ b/lib/chibi/regexp.scm @@ -102,8 +102,14 @@ (vector-length (regexp-match-matches md))) (define (regexp-match-name-offset md name) - (cond ((assq name (regexp-match-names md)) => cdr) - (else (error "unknown match name" md name)))) + (let lp ((ls (regexp-match-names md)) (first #f)) + (cond + ((null? ls) (or first (error "unknown match name" md name))) + ((eq? name (caar ls)) + (if (regexp-match-submatch-start+end md (cdar ls)) + (cdar ls) + (lp (cdr ls) (or first (cdar ls))))) + (else (lp (cdr ls) first))))) (define (regexp-match-ref md n) (vector-ref (regexp-match-matches md) diff --git a/tests/regexp-tests.scm b/tests/regexp-tests.scm index b62c1ac9..6396c20f 100644 --- a/tests/regexp-tests.scm +++ b/tests/regexp-tests.scm @@ -62,6 +62,16 @@ '(: (* (*$ (or "ab" "cd"))) "c") "abcdc") +(test "ab" + (regexp-match-submatch + (regexp-matches '(or (-> foo "ab") (-> foo "cd")) "ab") + 'foo)) + +(test "cd" + (regexp-match-submatch + (regexp-matches '(or (-> foo "ab") (-> foo "cd")) "cd") + 'foo)) + (test-re '("ababc" "abab") '(: bos ($ (* "ab")) "c") "ababc")