I want to match 2 numbers followed by a full stop followed by 2-4 more numbers. However when i compile am getting error : illegal escape character and its pointing to the full stop?
Any help appreciated, this is my first time using regular expressions in java, but was following api documentation and thought above was correct!
Remember that '\' escapes characters in Strings. So (even though your expression looks like a valid expression) you need to escape the escape character, so to speak. Try '\\.'
Great that worked- thanks! But i dont fully understand why. Is it because the regular expression is being defined in a string. If it wasn't in a string would a single backslash have worked?
If it wasn't in a string would a single backslash have worked?
I don't know how else you could describe a regular expression except in a String.
It works this way because somewhere in the Java API some piece of code is going through any string defined and looking for '\' because it is special. Remember that:
is a String containing the character ", whereas:
is a compiler error.
[ September 13, 2004: Message edited by: Paul Sturrock ] [ September 13, 2004: Message edited by: Paul Sturrock ]
Alan Moore
Ranch Hand
Joined: May 06, 2004
Posts: 262
posted
0
Originally posted by Paul Sturrock: I don't know how else you could describe a regular expression except in a String.
They could be read in from a file or the command line, or typed into a textfield (e.g., in a search/replace dialog). In any of those cases, a single backslash would suffice.