wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mock question  java.util.regex.Pattern - drag and drop 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 "Mock question  java.util.regex.Pattern - drag and drop" Watch "Mock question  java.util.regex.Pattern - drag and drop" New topic
Author

Mock question java.util.regex.Pattern - drag and drop

Sam Sunamin
Ranch Hand

Joined: Mar 16, 2007
Posts: 113
Required print out results:
Jack and Jxxx WENT UP THE hxxx.



Choice for drag as following:
\\w,(?a),ill,\\s,\\d,D,(?i)

The answer for it is (?i) ill \\d

Can anyone explain the answer for me? Or provide a link for me?

I am not sure about Pattern, especially about () and ? *.

Another example is (From Scanner API)



prints the following output:

1
2
red
blue

I don't know why it needs () for (\\d+), (\\w+)
[ April 12, 2007: Message edited by: Sam Sunamin ]

Yours Sam<br />SCJP5.0 97%<br />SCBCD5.0 72%
swarna dasa
Ranch Hand

Joined: Mar 15, 2007
Posts: 108
is for case insensitive match (Ignore case)
ill will match only hill and not JILL


Second example:- are you wondering why +?
if + is not there then \\d and \\w will match only one digit and one letter,
so the match will not be found



This will match

and not


in case you are wondering if it could be * then yes, * will work


Will give you the same output as +.
+ means one or more
* means zero or more.
Sam Sunamin
Ranch Hand

Joined: Mar 16, 2007
Posts: 113
Thank you Swarna.

Another Question to you. If I need to ignore case sensitive for "abc", is still used like (?i)abc, or what should it be used?

And I am wondering why they use parentheses in both cases.

Thank you.
sharan vasandani
Ranch Hand

Joined: Feb 22, 2007
Posts: 100
can you please explain (?i) a bit more,and yes why used brackets?
swarna dasa
Ranch Hand

Joined: Mar 15, 2007
Posts: 108
Parenthesis is used when it needs to be applied to the whole expression.
?iill
will be case insensitive (ignore case) only for the first letter.
The above expression will match ill or Ill, not ILL where all the letters are in uppercase.

Do we have ?i and the rest for the exam?
[ April 13, 2007: Message edited by: swarna dasa ]
Sam Sunamin
Ranch Hand

Joined: Mar 16, 2007
Posts: 113
If someone like to learn more about regular expression, using the link

http://java.sun.com/docs/books/tutorial/essential/regex/literals.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mock question java.util.regex.Pattern - drag and drop
 
Similar Threads
WhizLabs Regex Question
Doubt regarding regex
About regex
groupcount() method in MatchResult Interface in Java 5
scanner question