Skip to content

Instantly share code, notes, and snippets.

@Potherca
Last active December 10, 2019 20:24
Show Gist options
  • Save Potherca/1708b28f5515d0b56aa4e118d6c69086 to your computer and use it in GitHub Desktop.
Save Potherca/1708b28f5515d0b56aa4e118d6c69086 to your computer and use it in GitHub Desktop.
A list of capturing and non-capturing groups avaiable in regular expressions

Groups in regular expressions (Capturing and non-capturing)

In regular expressions groups can be used. Different types of groups have different notations.

These notations sometimes only differ as little as one character. This can be confusing.

To make it easier to distinguish between the different group types, they are all listen here.

Overview

The following groups are available in Regular expressions:

  • (…) Capturing group and (?<…>…) named capturing group
  • (?:…) Non-capturing group
  • (?!…) Negative Lookahead
  • (?=…) Positive Lookahead
  • (?<!…) Negative Lookbehind
  • (?<=…) Positive Lookbehind

Lookahead and lookbehind are also non-capturing groups. Together they are known as "lookarounds".

Syntax diagram of regular expression groups

group
= "("
(
/ "?"
(
"<" "…" ">" /* named capturing group */
/ ":" /* Non-capturing group */
/ "<"? /* Lookbehind */
(
"!" /* Negative Lookahead / Lookbehind */
/ "=" /* Positive Lookahead / Lookbehind */
)
)
)
"…"
")"
;
Display the source blob
Display the rendered blob
Raw
<svg xmlns="http://www.w3.org/2000/svg" class="railroad-diagram" style="background-color:#f5f2f0" viewBox="0 0 497 191">
<defs/>
<style>text{font:bold 14px monospace}</style>
<g fill="none" stroke="#000" stroke-width="3">
<path d="M20 21v20m10-20v20M20 31h20.5m-.5 0h0m416 0h0M40 31h10m0 0h0m28 0h0"/>
<rect width="28" height="22" x="50" y="20" fill="#cfc" rx="10" ry="10"/>
<text x="64" y="35" fill="#000" stroke="none" text-anchor="middle">(</text>
<path d="M78 31h10m0 0h0m272 0h0M88 31h20m0 0h116m0 0h116m0 0h20M88 31a10 10 0 0110 10v0a10 10 0 0010 10m0 0h0m232 0h0m-232 0h10m0 0h0m28 0h0"/>
<rect width="28" height="22" x="118" y="40" fill="#cfc" rx="10" ry="10"/>
<text x="132" y="55" fill="#000" stroke="none" text-anchor="middle">?</text>
<path d="M146 51h10m0 0h0m184 0h0m-184 0h20m0 0h0m144 0h0m-144 0h10m0 0h0m28 0h0"/>
<rect width="28" height="22" x="186" y="40" fill="#cfc" rx="10" ry="10"/>
<text x="200" y="55" fill="#000" stroke="none" text-anchor="middle">&lt;</text>
<path d="M214 51h10m0 0h10m0 0h0m28 0h0"/>
<rect width="28" height="22" x="234" y="40" fill="#cfc" rx="10" ry="10"/>
<text x="248" y="55" fill="#000" stroke="none" text-anchor="middle">…</text>
<path d="M262 51h10m0 0h10m0 0h0m28 0h0"/>
<rect width="28" height="22" x="282" y="40" fill="#cfc" rx="10" ry="10"/>
<text x="296" y="55" fill="#000" stroke="none" text-anchor="middle">&gt;</text>
<path d="M310 51h10m0 0h20m-184 0a10 10 0 0110 10v10a10 10 0 0010 10m0 0h58m28 0h58"/>
<rect width="28" height="22" x="234" y="70" fill="#cfc" rx="10" ry="10"/>
<text x="248" y="85" fill="#000" stroke="none" text-anchor="middle">:</text>
<path d="M320 81a10 10 0 0010-10V61a10 10 0 0110-10m-184 0a10 10 0 0110 10v50a10 10 0 0010 10m0 0h4m136 0h4m-140 0h0m68 0h0m-68 0a10 10 0 0010-10v0a10 10 0 0110-10m0 0h28m0 0a10 10 0 0110 10v0a10 10 0 0010 10m-68 0h20m0 0h0m28 0h0"/>
<rect width="28" height="22" x="200" y="110" fill="#cfc" rx="10" ry="10"/>
<text x="214" y="125" fill="#000" stroke="none" text-anchor="middle">&lt;</text>
<path d="M228 121h20m0 0h0m68 0h0m-68 0h20m0 0h0m28 0h0"/>
<rect width="28" height="22" x="268" y="110" fill="#cfc" rx="10" ry="10"/>
<text x="282" y="125" fill="#000" stroke="none" text-anchor="middle">!</text>
<path d="M296 121h20m-68 0a10 10 0 0110 10v10a10 10 0 0010 10m0 0h0m28 0h0"/>
<rect width="28" height="22" x="268" y="140" fill="#cfc" rx="10" ry="10"/>
<text x="282" y="155" fill="#000" stroke="none" text-anchor="middle">=</text>
<path d="M296 151a10 10 0 0010-10v-10a10 10 0 0110-10m4 0a10 10 0 0010-10V61a10 10 0 0110-10m0 0a10 10 0 0010-10v0a10 10 0 0110-10m0 0h10m0 0h0m28 0h0"/>
<rect width="28" height="22" x="370" y="20" fill="#cfc" rx="10" ry="10"/>
<text x="384" y="35" fill="#000" stroke="none" text-anchor="middle">…</text>
<path d="M398 31h10m0 0h10m0 0h0m28 0h0"/>
<rect width="28" height="22" x="418" y="20" fill="#cfc" rx="10" ry="10"/>
<text x="432" y="35" fill="#000" stroke="none" text-anchor="middle">)</text>
<path d="M446 31h10m0 0h20m-10-10v20m10-20v20"/>
</g>
</svg>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment