Complement of a Named Pattern Which Is a Character class
16:12 12 May 2026

Background

As I understand it, there are two ways to "reuse" capture groups in a regex:

  1. Subpattern: This reuses the pattern itself. I shall refer to this as "the pattern".

  2. Backreference: This references the text matched by the pattern. I shall refer to this as "the match".

Term Positional Named Example
Pattern (?[aeiou]).*\k a3di
Match ([aeiou]).*\1 (?[aeiou]).*(?&vowel) a3da

† I am unaware of any way to reuse the pattern by position.


Goal

I am trying to take the complement of a (named) subpattern. For example, I wish to create a capture group which is any character* that is not a .

* Ideally we restrict this to alphabetical characters, but I will settle for now.


Now this is easy enough if we want a static solution...

(?[^aeiou])

...but I want to be my "source of truth", so any change to the definition of a "vowel" is automatically reflected in . For example, if I include "y" among the vowels...

(?[aeiouy])

...then should automatically exclude "y".

Question

How can I define the capture group , such that it

  1. automatically complements the character class in , and

  2. can be reused throughout the regex, just like the static (?[^aeiou])?


I have made some naive attempts, with complements and negative lookarounds...

(?^(?&vowel))
(?[^(?&vowel)])
(?(?!(?&vowel)).)

...but these failed when I tested them.


Update

It seems I got the lookaround "hack" to actually work here, but I am still seeking a canonical solution.

(?(?!(?&vowel)).)
regex regex-negation capture-group character-class subpattern