phanthero
2021-4-20 22:02:01

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?


phanthero
2021-4-20 22:05:35

I thought this should only match infinite strings of a and b, like aaabbbb, bbbaaa, or bababaaaababbaa, or aaaababbbaaa, etc


samdphillips
2021-4-20 23:51:49

You’re matching zero or more ‘a’ or ‘b’ so a c matches zero.


samdphillips
2021-4-20 23:52:47

> (regexp-match #px"(a\|b)*" "ccc") '("" #f)


samdphillips
2021-4-20 23:53:26

If you use + it probably is doing more of what you expect > (regexp-match #px"(a\|b)+" "c") #f


ben.knoble
2021-4-21 01:07:36

And/or anchor the regex on the line: (regexp-match #rx"^(a\|b)*$" "c") is #false.