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

SCJP Regex question (page:526)

 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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.

 
Konstantinos Alvertis
Greenhorn
Posts: 11
Java
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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: 1077
Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks its displaying false.
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I am not getting the output 01234456
 
Konstantinos Alvertis
Greenhorn
Posts: 11
Java
  • 0
  • Mark post as helpful
  • send pies
  • 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? )
 
Robin Poulose
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Results
[Thumbnail for regex_1.png]
regex_2.png
java verion
[Thumbnail for regex_2.png]
 
Robin Poulose
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
http://www.coderanch.com/how-to/java/SCJP-FAQ#kb-regexp
 
Robin Poulose
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
opps!! thank you!!..!!didn't see it !!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic