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 regex to detect slashes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "regex to detect slashes" Watch "regex to detect slashes" New topic
Author

regex to detect slashes

auvrm papu
Ranch Hand

Joined: Sep 01, 2005
Posts: 105


I need a simple regex in JAVA to return true or false based on the input.

Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
What have you tried?
auvrm papu
Ranch Hand

Joined: Sep 01, 2005
Posts: 105
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

The starting and ending / are used in Perl regular expressions I think. In Java you don't need them; "[\\/]" will be enough.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
auvrm papu
Ranch Hand

Joined: Sep 01, 2005
Posts: 105
I tried that however, it detects only the "/" not the "\"

for example,



returns as valid string
auvrm papu
Ranch Hand

Joined: Sep 01, 2005
Posts: 105


actually worked

source:
http://www.regular-expressions.info/java.html

In literal Java strings the backslash is an escape character. The literal string "\\" is a single backslash. In regular expressions, the backslash is also an escape character. The regular expression \\ matches a single backslash. This regular expression as a Java string, becomes "\\\\". That's right: 4 backslashes to match a single one.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: regex to detect slashes
 
Similar Threads
regex to detect slashes
How to replace "\\" character.
String replaceAll method removes backslashes in replacement string
Compiling blank lines patterns using regex
in need of a regular expression (Java 1.5_11)