Background
As I understand it, there are two ways to "reuse" capture groups in a regex:
Subpattern: This reuses the pattern itself. I shall refer to this as "the pattern".
Backreference: This references the text matched by the pattern. I shall refer to this as "the match".
| Term | Positional | Named | Example |
|---|---|---|---|
| Pattern | † | (? |
a3di |
| Match | ([aeiou]).*\1 |
(? |
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
automatically complements the character class in
, andcan 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)).)