aspose file tools*
The moose likes Java in General and the fly likes regular expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "regular expression" Watch "regular expression" New topic
Author

regular expression

Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
how can I escape ".", "-", "_" in a regular expression?

when I do:



in a jsp I get:

Generated servlet error:
Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )


We must know, we will know. -- David Hilbert
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40

Originally posted by Joseph Sweet:
how can I escape ".", "-", "_" in a regular expression?


You have to remember that regular expressions are held in Java Strings in Java -- which IMO, makes it annoying to use.

So to escape ".", it is indeed "\."... but "\" has special meaning to a Java string, so you must escape that in order to pass it to the regex engine.... so it should be "\\.".

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
Thank you

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: regular expression