• 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
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Regex question

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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!
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic