• 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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

 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !!
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic