• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Regular expression: backslash inside bracket

 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 14501
325
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 "\\\\".
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 14501
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try the following.

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

FYI.. regex for "-" and "\" are "\\-" and "\\\\"
 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.

It works with:
String regularExp = "[0-9a-zA-ZæÆøØåÅ.@_\\-\\\\/]{1,50}";
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need to escape - if it's the very first character inside the character class.
 
I've never won anything before. Not even a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic