-
Notifications
You must be signed in to change notification settings - Fork 1
/
regexp.rkt
49 lines (38 loc) · 1.91 KB
/
regexp.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#lang racket/base
(module+ test
(require rackunit))
(define (ecma-262-regexp? x)
(string? x))
(provide ecma-262-regexp?)
;; Incomplete: this uses Racket regular expressions, not ECMA 262
;; regular expressions!
;;
;; Consider looking at: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
(define (ecma-262-regexp-matches? regex str)
(regexp-match? (pregexp regex) str))
(provide ecma-262-regexp-matches?)
(module+ test
(check-true (ecma-262-regexp-matches? "abc" "abc"))
(check-false (ecma-262-regexp-matches? "a" "b"))
(check-true (ecma-262-regexp-matches? "a" "ba"))
(check-true (ecma-262-regexp-matches? "a.*" "a"))
(check-true (ecma-262-regexp-matches? ".*a" "a"))
(check-true (ecma-262-regexp-matches? "^ag" "ag"))
(check-false (ecma-262-regexp-matches? "^ab" "a"))
(check-true (ecma-262-regexp-matches? "^r$" "r"))
(check-false (ecma-262-regexp-matches? "a" "A"))
;; Examples from https://spacetelescope.github.io/understanding-json-schema/reference/regular_expressions.html
(let ([r "^(\\([0-9]{3}\\))?[0-9]{3}-[0-9]{4}$"])
(check-true (ecma-262-regexp-matches? r "555-1212"))
(check-true (ecma-262-regexp-matches? r "(888)555-1212"))
(check-false (ecma-262-regexp-matches? r "(888)555-1212 ext. 532"))
(check-false (ecma-262-regexp-matches? r "(800)FLOWERS")))
(let ([uuid-regexp "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$"])
(check-true (ecma-262-regexp-matches? uuid-regexp "8f3ba6f4-5c70-46ec-83af-0d5434953e5f")))
(let ([nfs-regexp "^(/[^/]+)+$"])
(check-true (ecma-262-regexp-matches? nfs-regexp "/exports/mypath")))
(let ([dev-regexp "^/dev/[^/]+(/[^/]+)*$"])
(check-true (ecma-262-regexp-matches? dev-regexp "/dev/sda1")))
(check-true (ecma-262-regexp-matches? "foo" "foo"))
(check-true (ecma-262-regexp-matches? "foo" "afool"))
(check-false (ecma-262-regexp-matches? "^[^,]+$" "dear sir,")))