File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sam Sunamin
Ranch Hand
Posts: 113
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
swarna dasa
Ranch Hand
Posts: 108
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 113
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you please explain (?i) a bit more,and yes why used brackets?
 
swarna dasa
Ranch Hand
Posts: 108
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 113
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If someone like to learn more about regular expression, using the link

http://java.sun.com/docs/books/tutorial/essential/regex/literals.html
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic