This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
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 OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 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: 4101
    
  19

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 "\\\\".


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4541
    
    8

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: 4101
    
  19

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: 20136
    
  30

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 - OCEJPAD 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
 
It's not a secret anymore!