Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1032055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:01:34+00:00 2026-05-16T14:01:34+00:00

How are non-capturing groups, i.e., (?:) , used in regular expressions and what are

  • 0

How are non-capturing groups, i.e., (?:), used in regular expressions and what are they good for?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:01:35+00:00Added an answer on May 16, 2026 at 2:01 pm

    Let me try to explain this with an example.

    Consider the following text:

    http://stackoverflow.com/
    https://stackoverflow.com/questions/tagged/regex
    

    Now, if I apply the regex below over it (I did not escape the slashes for clarity; when using it, slashes would have to be escaped to \/ )…

    (https?|ftp)://([^/\r\n]+)(/[^\r\n]*)?      // slashes not escaped for clarity
    (https?|ftp):\/\/([^/\r\n]+)(\/[^\r\n]*)?   // slashes escaped
    

    … I would get the following result:

    Match "http://stackoverflow.com/"
         Group 1: "http"
         Group 2: "stackoverflow.com"
         Group 3: "/"
    
    Match "https://stackoverflow.com/questions/tagged/regex"
         Group 1: "https"
         Group 2: "stackoverflow.com"
         Group 3: "/questions/tagged/regex"
    

    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 (?:).

    (?:https?|ftp):\/\/([^/\r\n]+)(\/[^\r\n]*)?   // slashes escaped
    

    Now, my result looks like this:

    Match "http://stackoverflow.com/"
         Group 1: "stackoverflow.com"
         Group 2: "/"
    
    Match "https://stackoverflow.com/questions/tagged/regex"
         Group 1: "stackoverflow.com"
         Group 2: "/questions/tagged/regex"
    

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


    EDIT:

    As requested, let me try to explain groups too.

    Well, groups serve many purposes. They can help you to extract exact information from a bigger match (which can also be named), they let you rematch a previous matched group, and can be used for substitutions. Let’s try some examples, shall we?

    Imagine you have some kind of XML or HTML (be aware that regex may not be the best tool for the job, but it is nice as an example). You want to parse the tags, so you could do something like this (I have added spaces to make it easier to understand):

       \<(?<TAG>.+?)\> [^<]*? \</\k<TAG>\>
    or
       \<(.+?)\> [^<]*? \</\1\>
    

    The first regex has a named group (TAG), while the second one uses a common group. Both regexes do the same thing: they use the value from the first group (the name of the tag) to match the closing tag. The difference is that the first one uses the name to match the value, and the second one uses the group index (which starts at 1).

    Let’s try some substitutions now. Consider the following text:

    Lorem ipsum dolor sit amet consectetuer feugiat fames malesuada pretium egestas.
    

    Now, let’s use this dumb regex over it:

    \b(\S)(\S)(\S)(\S*)\b
    

    This regex matches words with at least 3 characters, and uses groups to separate the first three letters. The result is this:

    Match "Lorem"
         Group 1: "L"
         Group 2: "o"
         Group 3: "r"
         Group 4: "em"
    Match "ipsum"
         Group 1: "i"
         Group 2: "p"
         Group 3: "s"
         Group 4: "um"
    ...
    
    Match "consectetuer"
         Group 1: "c"
         Group 2: "o"
         Group 3: "n"
         Group 4: "sectetuer"
    ...
    

    So, if we apply the substitution string:

    $1_$3$2_$4
    

    … over it, we are trying to use the first group, add an underscore, use the third group, then the second group, add another underscore, and then the fourth group. The resulting string would be like the one below.

    L_ro_em i_sp_um d_lo_or s_ti_ a_em_t c_no_sectetuer f_ue_giat f_ma_es m_la_esuada p_er_tium e_eg_stas.
    

    You can use named groups for substitutions too, using ${name}.

    To play around with regexes, I recommend http://regex101.com/, which offers a good amount of details on how the regex works; it also offers a few regex engines to choose from.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason I can't seem to use non-capturing groups in MySQL. Is there
As explained in Can regular expressions be used to match nested patterns? , it
Referring to http://docs.python.org/howto/regex.html section Non-capturing and named groups , I saw an example whose
Is there an ability to make a lookahead assertion non-capturing? Things like bar(?:!foo) and
In the snippet below, the non-capturing group (?:aaa) should be ignored in the matching
With a string like HorsieDoggieBirdie, is there a non-capturing regex replace that would kill
I need to use some regular expressions to parse more complicated text and I
From the Java 6 Pattern documentation: Special constructs (non-capturing) (?: X )     X
Possible Duplicate: Non capturing group? I'm learning regex in JavaScript, and the (?:x) character
How can I remove capturing from arbitrarily nested sub-groups in a a Perl regex

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.