In the GWT tutorial where you build a stock watcher there is this regex expression to check if an input is valid:
if (!symbol.matches("^[0-9A-Z\\.]{1,10}$"))
Which allows inputs between 1 and 10 chars that are numbers, letters, or dots.
The part that confuses me is the \\.
I interpret this as escaped backslash \\ and then a . which stands for any character. And I thought the correct expression would be \. to escape the dot but doing this results in a regex error in eclipse Invalid escape sequence.
Am I missing the obvious here?
String x = "\.";isn't valid Java code.\.. And this thread is about the same issue:Java doesn't work with regex \s, says: invalid escape sequence.\\.is not an escaped backslash followed by a colon. As the others have mentioned, Java needs to escape backslash in Strings, so this is equivalent to\.as Regex. If you want to have an escaped backslash in Regex, you'd have to write it like this:\\\\.where each\\represents one backslash in the Regex.