-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
some regular expression usage examples
Ported from https://docs.racket-lang.org/reference/regexp.html Thanks Kinnard Hockenhull for the questions and feedback about regular expressions in Arc.
- Loading branch information
Showing
2 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
(require 'lib/re.arc) | ||
(suite re-match | ||
(suite simple-regex | ||
(test ex1 | ||
(assert-same '("a") | ||
(re-match (re "a|b") "cat"))) | ||
(test ex2 | ||
(assert-same '("a") | ||
(re-match (re "[at]") "cat"))) | ||
(test ex3 | ||
(assert-same '("caaat") | ||
(re-match (re "ca*[at]") "caaat"))) | ||
(test ex4 | ||
(assert-same '("caaat") | ||
(re-match (re "ca+[at]") "caaat"))) | ||
(test ex5 | ||
(assert-same '("ct") | ||
(re-match (re "ca?t?") "ct"))) | ||
(test ex6 | ||
(assert-same '("ca") | ||
(re-match (re "ca*?[at]") "caaat"))) | ||
(test ex7 | ||
(assert-same '("caa" "c" "aa") | ||
(re-match (re "(c*)(a*)") "caat"))) | ||
(test ex8 | ||
(assert-same '("t") | ||
(re-match (re "[^ca]") "caat"))) | ||
(test ex9 | ||
(assert-same '("cat" "a") | ||
(re-match (re ".(.).") "cat"))) | ||
(test ex10 | ||
(assert-same '("c") | ||
(re-match (re "^a|^c") "cat"))) | ||
(test ex11 | ||
(assert-same '("t") | ||
(re-match (re "a$|t$") "cat"))) | ||
(test ex12 | ||
(assert-same '("|") | ||
(re-match (re "\\|") "c|t"))) | ||
(test ex13 | ||
(assert-same '("ca") | ||
(re-match (re "[a-f]*") "cat"))) | ||
(test ex14 | ||
(assert-same '("]") | ||
(re-match (re "[]]") "c]t"))) | ||
(test ex15 | ||
(assert-same '("-") | ||
(re-match (re "[-]") "c-t"))) | ||
(test ex16 | ||
(assert-same '("[a]") | ||
(re-match (re "[]a[]+") "c[a]t"))) | ||
(test ex17 | ||
(assert-same '("a^") | ||
(re-match (re "[a^]+") "ca^t"))) | ||
(test ex18 | ||
(assert-same '("na") | ||
(re-match (re ".a(?=p)") "cat nap"))) | ||
(test ex19 | ||
(assert-same '("na") | ||
(re-match (re ".a(?!t)") "cat nap"))) | ||
(test ex20 | ||
(assert-same '("ap") | ||
(re-match (re "(?<=n)a.") "cat nap"))) | ||
(test ex21 | ||
(assert-same '("ap") | ||
(re-match (re "(?<!c)a.") "cat nap"))) | ||
(test ex22 | ||
(assert-same '("Ap") | ||
(re-match (re "(?i:a)[tp]") "cAT nAp"))) | ||
(test ex23 | ||
(assert-same '("ab") | ||
(re-match (re "(?(?<=c)a|b)+") "cabal"))) | ||
) | ||
(suite perl-regex | ||
(test ex1 | ||
(assert-same '("caa") | ||
(re-match (pre "ca{2}") "caaat"))) | ||
(test ex2 | ||
(assert-same '("caat") | ||
(re-match (pre "ca{2,}t") "catcaat"))) | ||
(test ex3 | ||
(assert-same '("cat") | ||
(re-match (pre "ca{,2}t") "caaatcat"))) | ||
(test ex4 | ||
(assert-same '("cat") | ||
(re-match (pre "ca{1,2}t") "caaatcat"))) | ||
(test ex5 | ||
(assert-same '("caat" "a") | ||
(re-match (pre "c(.)\\1t") "caat"))) | ||
(test ex6 | ||
(assert-same '("t ") | ||
(re-match (pre ".\\b.") "cat in hat"))) | ||
(test ex7 | ||
(assert-same '("ca") | ||
(re-match (pre ".\\B.") "cat in hat"))) | ||
(test ex8 | ||
(assert-same '("a") | ||
(re-match (pre "\\p{Ll}") "Cat"))) | ||
(test ex9 | ||
(assert-same '("!") | ||
(re-match (pre "\\P{Ll}") "cat!"))) | ||
(test ex10 | ||
(assert-same '("1ca") | ||
(re-match (pre "[a-f\\d]*") "1cat"))) | ||
(test ex11 | ||
(assert-same '(" h") | ||
(re-match (pre " [\\w]") "cat hat"))) | ||
(test ex12 | ||
(assert-same '("t\n") | ||
(re-match (pre "t[\\s]") "cat\nhat"))) | ||
(test ex13 | ||
(assert-same '("at") | ||
(re-match (pre "[[:lower:]]+") "Cat"))) | ||
)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ba23ab7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! Makes me want to try my hand at adding tests for
re-match*
re-pos
&re-subst