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

SCJP Regex question (page:526)

 
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dear Ranch,

I have query in the below code , which is mentioned in page:526 , I tried to compile it and run but the output I am getting is blank, but in the book the output answer is given option E: 01234456

Please assist.

 
Greenhorn
Posts: 11
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Vishal Hegde,

Did you tried it from command line? java Regex2 '\d*" ab34ef
 
Konstantinos Alvertis
Greenhorn
Posts: 11
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Konstantinos Alvertis wrote:Dear Vishal Hegde,

Did you tried it from command line? java Regex2 "\d*" ab34ef

 
Vishal Hegde
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks its displaying false.
 
Vishal Hegde
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But I am not getting the output 01234456
 
Konstantinos Alvertis
Greenhorn
Posts: 11
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Vishal Hegde,

You can try this: java Regex2 (\d*) ab34ef for windows works

( the double quotes  " are probably for linux , can anyone confirm this please? )
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am getting the following result while running in window 32bit and not in linux :

true
1234456

vishal got false may be becoz he didn't put double inverted commas!
22-05-2013-1-02-44-PM.png
[Thumbnail for 22-05-2013-1-02-44-PM.png]
 
Konstantinos Alvertis
Greenhorn
Posts: 11
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting!

Here i attached two screenshots of my cmd.

There is an "Known Issue" in the behaviour of Wildcard expansion here: http://www.oracle.com/technetwork/java/javase/7u4-relnotes-1575007.html

In detail:
[javadoc]Area: java command
Synopsis: Wildcard expansion behavior change on Windows platforms.

In the earlier JDK releases, a wildcard (* or ?) in double quotes on a DOS command line did not get expanded. For example, when using the following command:

java xxx "*.java"

the following string is passed to xxx:

*.java

In the current JDK release, wildcards in quoted arguments get expanded, and the matched file names (.java) are passed to xxx.

A workaround to produce the previous behavior is to escape the double quotes. For example, instead of the following command:

java xxx "*.java"

use:

java xxx \"*.java\"

Also ensure that your application code can accept both "*.java" as well as *.java as values.[/javadoc]

I use java version 7.

What java versions do you use?
regex_1.png
[Thumbnail for regex_1.png]
Results
regex_2.png
[Thumbnail for regex_2.png]
java verion
 
Robin Poulose
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Probably , you are correct ! its because of the versions.
Also. Why the Position 0 for the beginning of the string missing in the output for java 6 and 7?
is it a bug ?
FYI
I am using java version 6.

23-05-2013-1-30-45-AM.png
[Thumbnail for 23-05-2013-1-30-45-AM.png]
 
Konstantinos Alvertis
Greenhorn
Posts: 11
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is fine!
In the example, Vishal used find() method before the while loop! This was the first match. ( is the "true" that you printed first)
You can remove line 7 of the above code!
Here is a usefull link
https://coderanch.com/how-to/java/SCJP-FAQ#kb-regexp
 
Robin Poulose
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
opps!! thank you!!..!!didn't see it !!
 
reply
    Bookmark Topic Watch Topic
  • New Topic