Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"\d" vs "\\d" while tokenizing

 
Priyam Srivastava
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In K&B it is said that if "\d" doesn't work then try "\\d" while running the program. Though "\d" works fine in my sytem but when i tried "\\d" the o/p was different for the same program ...

import java.util.*;

class Tokens {
public static void main(String... args) {

String[] tokens = args[0].split(args[1]);
System.out.println("Total Tokens::" + tokens.length);
System.out.println("Tokens are::");
for(String s : tokens)
System.out.println(">" + s + "<");
}
}

O/P using "\d"::

F:\Priyam>java Tokens "abc qweq 3ed42r3 ewr4f42ff" "\d"
Total Tokens::8
Tokens are::
>abc qweq <
>ed<
><
>r<
> ewr<
>f<
><
>ff<

While O/P using "\\d" ::

F:\Priyam>java Tokens "abc qweq 3ed42r3 ewr4f42ff" "\\d"
Total Tokens::1
Tokens are::
>abc qweq 3ed42r3 ewr4f42ff<

Why is the JVM showing different behaviour with "\d" and "\\d" and not treating both as same??
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In K&B it is said that if "\d" doesn't work then try "\\d" while running the program.

Really ?? "\d" will look for a digit [0-9], while "\\d" will look for the string "\d".

Try
-> Tokens "abc1def\dghi" "\d"
and
-> Tokens "abc1def\dghi" "\\d"
 
Priyam Srivastava
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In K&B page 486 it is written in note::

Note:: Remember that to represent "\" in a string you may need to use the escape sequence "\\". Because of this, and depending on your OS, your second argument might have to be "\\d" or even "\\\\d".

so I still didn't get why treating diffrently !!
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Priyam,

it's not the VM that treats the argumets differently, it's the command-line. With some command-lines, the backslash is a meta character (like the backslash in Java strings). And in order to pass a backslash to your program, you must enter two (or more) backslashes on the command line.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In K&B it is written "and depending on your OS" so to see the difference you should try it on different operating systems and with different (as Manfred pointed out) command line shells.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic