aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regex question 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 "Regex question" Watch "Regex question" New topic
Author

Regex question

megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206
Hi all,

Please help me understand the answer to this question from JQPlus...

Which of the following patterns will correctly capture all Hex numbers that are delimited by atleast one whitespace in an input text?
1) \s*0[xX][0-9a-fA-f]+\s*
2) \s0[x][X][0-9a-fA-f]+\s
3) \s*0[xX][0-9a-fA-f]*\s*
4) 0[xX][0-9a-fA-f]+\s
5) \s0[xX][0-9a-fA-f]?\s

They ask to choose two options and the correct answers they give
as 1 and 2. But they also give an explanatio as to why
1 is incorrect. I don't quite get it.
Can anyone please explain....
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

Which of the following patterns will correctly capture all Hex numbers that are delimited by atleast one whitespace in an input text?
1) \s*0[xX][0-9a-fA-f]+\s*
2) \s0[x][X][0-9a-fA-f]+\s
3) \s*0[xX][0-9a-fA-f]*\s*
4) 0[xX][0-9a-fA-f]+\s
5) \s0[xX][0-9a-fA-f]?\s


Option 1: a hex number that starts with 0 or more spaces with x or X prefix and 0-9 or a-f or A-F occurrence one or more and ends with 0 or more spaces.

I don't think option 2 correct because it says x and X both, erroneous.

Option 4 looks good, but it must end with a space

Option 5 is not true because it write ? at the end (means 0 occurrence of 0-9, a-f or A-f will do, that is not true.


I find option one correct.

Please correct me if I miss something there!


cmbhatt
megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206
Hi,

Thanks for helping as always

But need more help....Option 1 is not correct ...
It matches this " 0xX111 " in the following code which is not nice
I wrote this code



Also if I change line 1 to
Pattern p = Pattern.compile("\\s0[xX][0-9a-fA-f]+\\s"); //line 1
OR
Pattern p = Pattern.compile("\\s0[xX][0-9a-fA-f]+\\s*"); //line 1

I get correct answer...

Now I also found that the problem above occurs only when I write the first
part of the regex as "\\s*" instead of "\\s"

Can you please help me find the reason...

Thanks,
Megha
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Ummmm!

Adding * to \\s causes confusion, wrong result! Otherwise correct code as you have worked with that well.

Working on it....

Mind boggling!!!
[ April 29, 2007: Message edited by: Chandra Bhatt ]
[ April 29, 2007: Message edited by: Chandra Bhatt ]
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hey Megha Got It!

It is due to heck A-f
Pattern p = Pattern.compile("\\s*0[xX][0-9a-fA-f+\\s*"); //line 1

It should be A-F

X is within range of A and f.
That is why we were getting wrong output.

Very funny!!!

Anyways Megha, if you look at my top most reply, what I talked about;
In every options I wrote A-F only but overlooked the options detail where everywhere it is A-f :-)
[ April 29, 2007: Message edited by: Chandra Bhatt ]
megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206
Hey Chandra Thats indeed interesting...Good finding...But still I dont find peace with this expression...

If I change line 1 to
Pattern p = Pattern.compile("\\s0[xX][0-9a-fA-f]+\\s"); //line 1
OR
Pattern p = Pattern.compile("\\s0[xX][0-9a-fA-f]+\\s*"); //line 1

I get correct answer...

Now I also found that the problem above occurs only when I write the first
part of the regex as "\\s*" instead of "\\s"


Still cant find how removing the * doesnt match with X in A-f.

But I think it won't be in scope of the exam...so leaving it for now.
If you know on the top of your mind let me know else don't waste more
time on this question...
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

If you know on the top of your mind let me know else don't waste more
time on this question...


Top of my mind is blank.

If you know the reason, please let me know that. I can't leave anything in the middle.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regex question