File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why java doesnt allow "\" ?

 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in java

where as

 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The backslash is a special character. A character preceeded by a "\" is a escape sequence. Please take a look here for more information.
 
Winston Gutkowski
Bartender
Pie
Posts: 9497
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roberto Perillo wrote:The backslash is a special character. A character preceeded by a "\" is a escape sequence.

Actually, the problem is that backslash is a special character for both Strings and regexes, and each define their own escape sequences.

\d{10} is certainly a valid regex, but the trouble is that regexes are also Strings, and \d is NOT a valid escape sequence for a String.

Therefore, you have to double up the \ so that the regex processor gets to see it, viz:
String regex = "\\d{10}";

Personally, I think they would have saved an awful lot of bother by choosing a different escape character for Strings.

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Roberto Perillo wrote:The backslash is a special character. A character preceeded by a "\" is a escape sequence.

Actually, the problem is that backslash is a special character for both Strings and regexes, and each define their own escape sequences.

\d{10} is certainly a valid regex, but the trouble is that regexes are also Strings, and \d is NOT a valid escape sequence for a String.

Therefore, you have to double up the \ so that the regex processor gets to see it, viz:
String regex = "\\d{10}";


Actually, it's a special character in String literals, that is, quoted strings in your source code.

So in the above, the compiler sees "\\" and says, "Oh, the first \ is escaping the second one, so I will create a String with the characters '\', 'd', '{', '1', '0', '}' in it." If we were, say, reading the String from a file, then we could simply put

\d{10}

in the file, and something like


and end up with the same results--that is, in both cases, the String contains the characters:
\d{10}

 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:Personally, I think they would have saved an awful lot of bother by choosing a different escape character for Strings.

I quite like the C# feature of being able to prefix string literals with @ to disable escaping - it comes in very handy for regular expressions and Windows file paths. E.g.:
String regex = @"\d{10}";
 
Winston Gutkowski
Bartender
Pie
Posts: 9497
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:Actually, it's a special character in String literals...

Yes. Quite right.

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:
Winston Gutkowski wrote:Personally, I think they would have saved an awful lot of bother by choosing a different escape character for Strings.

I quite like the C# feature of being able to prefix string literals with @ to disable escaping - it comes in very handy for regular expressions and Windows file paths. E.g.:
String regex = @"\d{10}";


That is handy, and, IMHO, a better solution than picking some other escape character (which could then end up needing to be double escaped in some other context).
 
Winston Gutkowski
Bartender
Pie
Posts: 9497
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:I quite like the C# feature of being able to prefix string literals with @ to disable escaping - it comes in very handy for regular expressions and Windows file paths.

Indeed. I wonder why they haven't included it? Perhaps there's an aversion to accepting anything from Microsoft.

Winston
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64204
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because outside of Windows file paths, \ is rarely used.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Because outside of Windows file paths, \ is rarely used.


Hmm... It seems to me there's some other context in which we regularly need that character to express something, but I just can't remember what it is...
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The meaning of \ as an escape character in string literals is something that Java has inherited from C and C++.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic