Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Regular Expression for escaping periods

807603Jan 31 2008 — edited Jan 31 2008
Hi,

I'm trying to use the RegEx method of the String class to match a value. The trouble is the String value I'm trying to match are periods, which are as you know the predefined "any" character for matching. http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html
How do I successfully escape this so it will hunt for a period? I assumed I could escape it with the backslash \. but my compiler shows an error as: "Illegal escape character in String literal".

Take for example:
// sample string is a bad email address with 2 periods.
String = email_address = johndoe@sun..com;

boolean is_bad_email_email = email_address.matches("\.\.");

When I test the RegEx it works stand alone, but why won't it let me escape the period in the above example? Is there another way to do this?

Thanks!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 28 2008
Added on Jan 31 2008
5 comments
1,332 views