I've a rather simple regex that works perfectly fine in my Ruby code but refuses to work in my Lisp code. I'm just trying to match a URL (slash followed by a word, and no more). Here's the regex I have that works in Ruby: ^\/\w*$
I'd like this to match "/"
or "/foo"
but not "/foo/bar"
I've tried the following:
(cl-ppcre:scan "^/\w*$" "/") ;works
(cl-ppcre:scan "^/\w*$" "/foo") ;doesn't work!
(cl-ppcre:scan "^/\w*$" "/foo/bar") ;works, ie doesn't match
Can someone help?
The backslash (\) character is, by default, the single escape character: It prevents any special processing to be done to the character following it, so it can be used to include a double quote (
"
) inside of a string literal like this"\""
.Thus, when you pass the literal string
"^/\w*$"
tocl-ppcre:scan
, the actual string that is passed will be"^/w*$"
, i.e. the backslash will just be removed. You can verify this by evaluating(cl-ppcre:scan "^/\w*$" "/w")
, which will match.To include the backslash character in your regular expression, you need to quote it like so:
"^/\\w*$"
.If you work with literal regular expressions a lot, the required quoting of strings can become tedious and hard to read. Have a look at CL-INTERPOL for a library that adds a nicer syntax for regular expressions to the Lisp reader.