Hi,
I am using Eclipse, Java SDK 1.4 and the java.uitl.regex
I am trying to using regular expressions in my code
I could write something simple like:
String re = "\d";
And I am receiving an invalid escape sequence error.
And I am not so sure why
Aidy
BartCr - 02 Dec 2005 12:56 GMT
If you would like to match any digit, write:
String re = "\\d";
>From the API:
Backslashes within string literals in Java source code are interpreted
as required by the Java Language Specification as either Unicode
escapes or other character escapes. It is therefore necessary to double
backslashes in string literals that represent regular expressions to
protect them from interpretation by the Java bytecode compiler. The
string literal "\b", for example, matches a single backspace character
when interpreted as a regular expression, while "\\b" matches a word
boundary. The string literal "\(hello\)" is illegal and leads to a
compile-time error; in order to match the string (hello) the string
literal "\\(hello\\)" must be used.
If you would like to match the string \d, write
String re = "\\\\d";
Bart
Thomas Weidenfeller - 02 Dec 2005 13:00 GMT
> I could write something simple like:
>
[quoted text clipped - 3 lines]
>
> And I am not so sure why
A backslash in a string literal need to be escaped with another
backslash to get it into the string as a single backslash.
/Thomas

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
Roedy Green - 02 Dec 2005 18:28 GMT
>String re = "\d";
>
>And I am receiving an invalid escape sequence error.
see http://mindprod.com/jgloss/regex.html
that should be "\\d";

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.