Regexdrill

Groups and lookarounds Updated November 2026

Non-capturing group (?:...)

(?:...) groups without capturing: it lets you apply a quantifier or an alternation to a sequence, (?:ab)+ or (?:cat|dog)s, without creating a numbered group. Use it whenever you do not need the text back; it keeps group numbers stable and is marginally faster.

Syntax

(?:pattern)

Examples

"https://x.com".match(/^(https?):\/\/(?:www\.)?(.+)$/)

Result: ["https://x.com", "https", "x.com"]

The optional www. is grouped but not captured, so the host is still group 2.

"1.2.3.4".match(/^(?:\d+\.){3}\d+$/)[0]

Result: "1.2.3.4"

"cats dogs".match(/(?:cat|dog)s/g)

Result: ["cats", "dogs"]

["cats".match(/(cat|dog)s/).length, "cats".match(/(?:cat|dog)s/).length]

Result: [2, 1]

Capturing adds an entry to the match array; non-capturing does not.

Common mistakes

  • Typing (:?...) or (?...) by mistake; the colon follows the question mark.
  • Capturing everything and then renumbering $3 to $4 each time you add a group; capture only what you read.
  • Expecting (?:) to change what matches; it only changes what is captured.
  • In Python, re.findall returns groups instead of whole matches whenever a capturing group exists; make helper groups non-capturing.

Practice Non-capturing group (?:...)

These drills check your answer on live data, free, no account needed.

  • Group without capturingEach match is one or more copies of ab or cd, immediately followed by a number. Capture only the number in group 1.
  • Alternation inside a groupFind a number followed by a unit, with or without one space between them, and capture the unit in group 1. Units are kg, g, lb and oz, and the unit must end the word.
  • Repeated group: dotted numbersThe whole line must match. Match one or more numbers separated by single dots, like 1, 1.2 or 10.0.3. No leading, trailing or doubled dots.

Questions

What is the difference between (...) and (?:...)?

Both group. (...) also stores the matched text as a numbered capture for later use; (?:...) does not. Everything else, including quantifiers and alternation, works the same.

Is a non-capturing group faster?

Slightly, because the engine skips recording the capture. The difference is rarely measurable; use it for clarity and stable numbering rather than speed.

Can I name a non-capturing group?

No. Names only apply to capturing groups, written (?<name>...). If you need a name, you need a capture.

Related

  • Capture groupsHow parentheses capture text, reading groups in JavaScript via match, matchAll and exec, reusing them with $1 in replace, and the g trap that loses groups.
  • Alternation |How the pipe works, why ^cat|dog$ does not do what it looks like, how to group alternatives, and why option order matters when one is a prefix of another.
  • Named groups (?<name>...)How to capture by name, read match.groups.name, use $<name> in replacements and \k<name> as a backreference, and the (?P<name>) syntax Python needs.
  • QuantifiersWhat * + ? {n} {n,} and {n,m} mean, how greedy matching works, how to repeat a group instead of a single character, and the silent {2, 3} mistake.

Previous: Capture groupsNext: Named groups (?<name>...)