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: backslash inside bracket Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regular expression: backslash inside bracket" Watch "Regular expression: backslash inside bracket" New topic
Author

Regular expression: backslash inside bracket

Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
Hi.

I want to make a regular expression which allow the user to type a slash and a backslash in the word.

If I put a backslash inside the bracket it won´t work (compile).

Doesn´t work with '\\'
String regularExp = "[0-9a-zA-ZæÆøØåÅ.@_-\\]{1,50}";

Doesn´t work with '\/'
String regularExp = "[0-9a-zA-ZæÆøØåÅ.@_-\/]{1,50}";

It works without any '\'
String regularExp = "[0-9a-zA-ZæÆøØåÅ.@_-]{1,50}";

Any help would be appreciated.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3370
    
    9
I believe you don't have to escape '/'. If you want a backslash, you need to write "\\\\". Why? The regex API needs you to escape \ with \. So it expects \\, right?

Now, how are you going to represent \\ as a Java String? Remember that Java itself requires backslashes to be escaped as well, so each '\' needs another '\'. The result is "\\\\".
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

Remember that \ is a special character in both regular expressions and Java string literals. Which means that to use it as a normal backslash character, you need to escape it twice.
Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
Hi.

I just tried with 4 backslashes as you suggested: '\\\\':

String regularExp = "[0-9a-zA-ZæÆøØåÅ.@_-\\\\]{1,50}";

This doesn´t work either. The error is:

java.util.regex.PatternSyntaxException: Illegal character range near index 21
[0-9a-zA-Zµã°ÏÕ┼.@_-\\]{6,50}
^
at java.util.regex.Pattern.error(Pattern.java:1650)
at java.util.regex.Pattern.range(Pattern.java:2272)
at java.util.regex.Pattern.clazz(Pattern.java:2213)
at java.util.regex.Pattern.sequence(Pattern.java:1727)
at java.util.regex.Pattern.expr(Pattern.java:1687)
at java.util.regex.Pattern.compile(Pattern.java:1397)
at java.util.regex.Pattern.<init>(Pattern.java:1124)
at java.util.regex.Pattern.compile(Pattern.java:817)

Any idea what is wrong?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3370
    
    9
That's because you forgot to escape the '-' character, so the regex compiler expects a range.

[edit]

Take a look at Pattern.quote(). You can quote all your separate characters before you put them in the character class:
Siva Vulchi
Ranch Hand

Joined: Nov 17, 2011
Posts: 30
Try the following.

String regularExp = "[0-9a-zA-ZæÆøØåÅ.@_\\-\\\\/]{1,50}";

FYI.. regex for "-" and "\" are "\\-" and "\\\\"
Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
Thanks.

It works with:
String regularExp = "[0-9a-zA-ZæÆøØåÅ.@_\\-\\\\/]{1,50}";
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

You don't need to escape - if it's the very first character inside the character class.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regular expression: backslash inside bracket
 
Similar Threads
Validating multiple email addresses in GWT
Regular Expressions in Java
About Java Script Regular Expressions
Regular Expression?
regular expression email validation with wild card