• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

RegEx

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a question about RegEx. The following question is from Examulator mock-up,



I do not understand how o/p is obtained as rapraprerap. Can anybody shed some light on this. Thanks.
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Venkat Sidh:
Have a question about RegEx. The following question is from Examulator mock-up,



I do not understand how o/p is obtained as rapraprerap. Can anybody shed some light on this. Thanks.




The regular expression pattern is a letter "r", followed by a required word character, followed by an optional letter "p". The pattern will match 4 times...

  • "rap" from the first word
  • "rap" from the beginning of the second word "rapture"
  • "re" from the end of second word
  • "rap" from the end of the third word "wrap"


  • Henry
     
    Venkat Sidh
    Ranch Hand
    Posts: 61
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Henry,

    Thanks for explanatory answer.

    The regular expression pattern is a letter "r", followed by a required word character, followed by an optional letter "p". The pattern will match 4 times...



    Actually I'm confused about ? in expression, does ? (greedy quantifier) denote zero or one instance of character p?
     
    Sheriff
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Venkat Sidh:
    ...Actually I'm confused about ? in expression, does ? (greedy quantifier) denote zero or one instance of character p?


    Yes, this is why it's matching "re" from the end of the second word.
  • r matches r
  • \\w matches the "word character" e
  • p? matches zero occurrences of p
  • So the match here is just re.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic