• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java regular expression (.) dot pattern

 
Mark Malin
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to match a . (dot), which is a meta character. But the following did not compile because \. is not a valid escape sequence.
java.util.regex.Pattern.compile("[0-9]*\.");
Any get-around?
Thx.
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Mark Malin
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic