aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark ""\d" vs "\\d" while tokenizing" Watch ""\d" vs "\\d" while tokenizing" New topic
Author

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

Priyam Srivastava
Ranch Hand

Joined: Oct 29, 2006
Posts: 169
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??


"History would be kind to me, for I intend to write it."
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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"


[My Blog]
All roads lead to JavaRanch
Priyam Srivastava
Ranch Hand

Joined: Oct 29, 2006
Posts: 169
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

Joined: Jun 04, 2007
Posts: 377
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

Joined: Aug 03, 2002
Posts: 7729
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "\d" vs "\\d" while tokenizing
 
Similar Threads
split() method problem
scann and split issue
SPLIT METHOD IN STRING CLASS
about String.split execution
June Newsletter Puzzle