summaryrefslogtreecommitdiff
path: root/bs/re.scm
diff options
context:
space:
mode:
authorBen Sima <ben@bsima.me>2020-05-19 17:25:49 -0700
committerBen Sima <ben@bsima.me>2020-05-25 19:48:20 -0700
commit96245a828aa9c717b847812812546667e090ee3f (patch)
tree6dbdf15a70844c388735a76b2f7e86b5e7c54ac6 /bs/re.scm
parentf84e431623285ef12c750fd3be58e37fda9893e8 (diff)
Add more re functions and docs
Diffstat (limited to 'bs/re.scm')
-rw-r--r--bs/re.scm58
1 files changed, 51 insertions, 7 deletions
diff --git a/bs/re.scm b/bs/re.scm
index 6ecc074..e2bc276 100644
--- a/bs/re.scm
+++ b/bs/re.scm
@@ -1,11 +1,55 @@
#!r6rs
(library (bs re (0))
- (export match group)
+
+ ;; Similar to Python's `re` module. One big difference is that the
+ ;; string object to be searched is always the first argument to a
+ ;; function. This allows us to pipe a string through multiple
+ ;; transformations. For example:
+ ;;
+ ;; (-> " string "
+ ;; string.strip
+ ;; (re.search "in")
+ ;; (re.group 1))
+ ;;
+ ;; TODO: consider putting the string as the last argument, to fit with
+ ;; other container types like `list`, then use ->>
+ ;; TODO: port srfi-115 to guile
+ ;; TODO: make `match` and `search` do different things
+
+ (export match group sub search
+ I IGNORECASE M MULTILINE)
(import (rnrs base (6))
(ice-9 regex))
- (define (match s pat)
- (string-match pat s))
- (define (group m n)
- (if m
- (match:substring m n)
- #f)))
+
+ (define I regexp/icase)
+ (define IGNORECASE regexp/icase)
+ (define M regexp/newline)
+ (define MULTILINE regexp/newline)
+
+ ;; Compile `pattern` into a regular expression object.
+ (define (compile pattern . flags)
+ (apply make-regexp (cons pattern flags)))
+
+ ;; If zero or more characters at the beginning of `string` match the
+ ;; regular expression `pattern`, return a corresponding match object.
+ (define (match string pattern)
+ (regexp-exec pattern string))
+
+ ;;
+ (define (group match-obj n)
+ (if match-obj
+ (match:substring match-obj n)
+ #f))
+
+ ;; Return the string obtained by replacing the leftmost
+ ;; non-overlapping occurrences of `pattern` in `string` by the
+ ;; replacement `repl`. If the pattern isn’t found, string is returned
+ ;; unchanged.
+ (define (sub string pattern repl)
+ (regexp-replace pattern string repl))
+
+ ;; Scan through `string` looking for the first location where the
+ ;; regular expression `pattern` produces a match, and return a
+ ;; corresponding match object. Returns `#f` if no match is found.
+ (define (search string pattern)
+ (regexp-exec pattern string)))