• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

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: 23907
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.
     
    You showed up just in time for the waffles! And this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic