Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are at least four reasons for using a non-capturing group:</p> <p>1) <strong>Save Memory</strong>: When you match a capturing group, the group's content is stored independently in memory, whether you need it or not. That space in memory can add up quickly when you're using regex and storing the results on a large set of data. For instance, <code>[0-9]+(, [0-9]+)*</code> will match a series of integers separated by commas and spaces like <code>15, 13, 14</code>. Let's assume you only need whole matching string from the result (group 0). In this case, though, you'll really be storing <code>"15, 13, 14"</code> and <code>", 14"</code>, since the latter is in a captured group. You can save memory and time by using <code>[0-9]+(?:, [0-9]+)*</code> instead. It might not matter for such a simple and short example, but with more complicated regexes, those extra bits of memory usage add up fast. As a bonus, <em>non-capturing groups are also faster to process.</em></p> <p>2) <strong>Simpler Code</strong>: If you've got a regex like <code>([a-z]+)( \.)* ([a-z]+) ([a-z]+)</code> and want to extract the three words, you'd need to use groups 1, 3, and 4. While that's not terribly difficult, imagine that you need to add another group between the latter two words like <code>([a-z]+)( \.)* ([a-z]+)( \.)* ([a-z]+)</code>. If you use these groups in several places later in your code, it may be hard to track them down. Instead, you can first write <code>([a-z]+)(?: \.)* ([a-z]+) ([a-z]+)</code> at first, and then change it to <code>([a-z]+)(?: \.*) ([a-z]+)(?: \.)* ([a-z]+)</code>, both of which match the words to groups 1, 2, and 3 respectively.</p> <p>3) <strong>External Dependencies</strong>: You might have a function or library which needs to receive a regex match with exactly n groups. This is an unusual instance, but making all the other groups non-capturing will satisfy the requirement.</p> <p>4) <strong>Group Count Limits</strong>: Most languages have a limit to the overall number of capturing groups in a regex. It's unusual to need that many groups (100 for python, for instance), it is possible. You can use fewer groups and run up against this limit less frequently by using non-captured groups which are not limited in that way. For instance:</p> <pre><code>((one|1), )((two|2), )…((nine_hundred_ninety_nine|999), ) </code></pre> <p>where the <code>…</code> is all the in-between groups wouldn't match in some languages because it has too many capturing groups. But:</p> <pre><code>(?:(one|1), )(?:(two|2), )…(?:(nine_hundred_ninety_nine|999), ) </code></pre> <p>would match and still return all the groups like <code>one</code> or <code>22</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload