Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2005

Tip: Looking for answers? Try searching our database.

RegEx !compile

Thread view: 
aidy - 02 Dec 2005 12:00 GMT
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.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.