I am new to regular expressions (and to java), so this is probably a simple question.
I am trying to match the character { at the end of a line. My attempts are simply this:
row.matches("{$")
row.matches("\{$")
But both just give
Exception in thread “main” java.util.regex.PatternSyntaxException: Illegal repetition
What am I doing wrong?
You simply need to escape the {, since it’s a metacharacter. Because Java reserves a single backslash for special contexts (\n, \r, etc.), two backslashes are required to generate one backslash for the Pattern. Therefore,
will properly evaluate to
Not only this, but the
matchesmethod checks to see iff the entire string matches, instead of just a subset. Hence, the^.*part