• 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 Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Regular Expressions - Capturing Groups Confusion

 
Greenhorn
Posts: 19
IntelliJ IDE Tomcat Server Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,

I am reading Oracle Tutorial on Regular Expressions. I am on the topic Capturing Groups.

Though the reference is excellent but except that a parenthesis represents a group, i am finding many difficulties in understanding the topic. Here are my confusions.

  • What is the significance of counting groups in an expression?


  • I don't understand what are non-capturing groups?


  • Can someone elaborate them with examples?

    Thanks,
     
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    example and explain :



    the Pattern (a.a) Looking for any tokens Consists of three characters but must start with (a) and end of (a) Regardless of what the middle maybe different character , space ,symbol don't care what in between it
    so when run this code the result shoud be



    So here Group refer to which part of Matcher i meaning (abaaba) : Applies condition Regular Expressions(a.a) and founded .
     
    Ben Prequel
    Greenhorn
    Posts: 19
    IntelliJ IDE Tomcat Server Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Jordan,

    Thanks for reverting back. I don't think it explains what i am asking. I know, what a group is. What i was asking was, what's the significance of capturing a group
    and what are non-capturing groups?

    Thanks,
     
    mohammad jordan
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    ok sry .. any way Let me try to explain this with an example...

    Consider the following text:



    Now, if I apply the regex below over it...



    ... I would get the following result:






    But I don't care about the protocol -- I just want the host and path of the URL. So, I change the regex to include the non-capturing group (?.




    Now, my result looks like this:



    See !! ? The first group has not been captured. The parser uses it to match the text, but ignores it later, in the final result.







     
    Ranch Hand
    Posts: 774
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Dear Ben,

    For this question, you can have a detailed answer at What is a Non Capturing Group?.
    If any further confusion, feel free to ask.

    BR,
     
    Prithvi Sehgal
    Ranch Hand
    Posts: 774
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    mohammad jordan wrote: ok sry .. any way Let me try to explain this with an example...

    Consider the following text:



    Now, if I apply the regex below over it...



    ... I would get the following result:






    But I don't care about the protocol -- I just want the host and path of the URL. So, I change the regex to include the non-capturing group (?.




    Now, my result looks like this:



    See !! ? The first group has not been captured. The parser uses it to match the text, but ignores it later, in the final result.









    Dear Jordan,

    Kindly QuoteYourSources when you provide an answer to the thread. This answer seems a copy from StackOverFlow and i have mentioned the
    thread in the reply.

    Thanks,
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic