Was writing a simple program that outputs whether or not the input string matches the given regular expression, as so: #lang racket
(for ([line (in-lines)])
(when (regexp-match #px"(a\|b)*" line) (displayln line))) But for some bizarre reason, when I run racket reg.rkt, giving c as input makes the program output c as well? Is there something wrong with my regex?
I thought this should only match infinite strings of a and b, like aaabbbb, bbbaaa, or bababaaaababbaa, or aaaababbbaaa, etc
You’re matching zero or more ‘a’ or ‘b’ so a c matches zero.
> (regexp-match #px"(a\|b)*" "ccc")
'("" #f)
If you use + it probably is doing more of what you expect > (regexp-match #px"(a\|b)+" "c")
#f
And/or anchor the regex on the line: (regexp-match #rx"^(a\|b)*$" "c") is #false.