How can I escape the forward slashes in the regex when using the matches constraint? This is what I tried:
constraints {
url (
matches: "^http://www.google.com/$"
)
}
Error: solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}"
constraints {
url (
matches: "^http:\/\/www.google.com\/$"
)
}
Error: unexpected char: '\'
In strings defined with double quotes (
".."
) groovy replaces variables with$
.In your validation regex this is causing an error. You want to use the
$
for a regular expression and not for variable replacement. To avoid variable replacement you can define strings in single quotes ('..'
)You don't need to escape
/
when defining the regular expression inside a string but you you should escape.
. In a regular expression.
matches any character. So the regular expression^http://www.google.com/$
matcheshttp://wwwAgoogleB.com/
.To escape a character inside a string you have to use
\\
(the first\
is for escaping the second\
). So the following expression should work:Normally you could also use the groovy regular expression syntax (
/../
). It this case the regular expression would look like thisYou don't need double backslashes for escaping but therefore you have to escape slashes (because they are used for terminating the regular expression). But as far as I know this syntax does not work with the matches constraint from grails.