jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes Java regular expression (.) dot pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java regular expression (.) dot pattern" Watch "Java regular expression (.) dot pattern" New topic

Java regular expression (.) dot pattern

Mark Malin

Joined: Jun 03, 2003
Posts: 3
I tried to match a . (dot), which is a meta character. But the following did not compile because \. is not a valid escape sequence.
Any get-around?

Charles Lyons
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
I would have thought that as a fully-qualified Java man you would have got this one!!!
It's actually very simple. You need to end up with the escape sequence \. for the regexp to the valid, but it doesn't work because the Java compiler sees it as an escape for its String objects, while a full-stop here does not require escaping - if you see what I mean.
The work-around is to write "\\.". This way, the backslash is escaped on the first round (remember that a backslash must be escaped anyway), and on the second round (when involving regex), it is the full-stop which is escaped...
Hope this helps!
[ June 09, 2003: Message edited by: Charles Lyons ]

Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Mark Malin

Joined: Jun 03, 2003
Posts: 3
Thanks Charles! It did occured to me at one point. But I did not give it a try, because I thought the Java's escape sequence is different from the pattern meta char set. Stupid me.
I agree. Here's the link: http://aspose.com/file-tools
subject: Java regular expression (.) dot pattern
It's not a secret anymore!