• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you please explain (?i) a bit more,and yes why used brackets?
 
swarna dasa
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
permaculture is largely about replacing oil with people. And one tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic