File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 18545
    
  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

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: regular expression
 
Similar Threads
Validating domain name with struts validator
Invalid RegEx on @Pattern
Regular Expression
URL Validation
masking validation problem