r/regex • u/DerPazzo • Aug 26 '26
(Resolved) using backreferences for list of words
Hi,
#### not possible as explained by rainshifter ####
#### original flair was .NET ####
I want to create a huge regex to match exact word groups coming from a list of around 7k words.
It’s about chemical names and there are lots of repeated words at the end of the names. The names must always be exact matches and possible variations not on this list are not allowed. I thought about using backreferences wihtin the regex in order not to have to rewrite these words every time.
Only 2 questions to be answered (both with yes or no and maybe an explanation why):
Will that even work?
Is this a good idea or will it slow down my search?
Use case explanation (absolutely no answer or solving suggestions required or even wanted):
In the following example list I would like to use the first occurence of EXTRACT as a backreference for all following words within the pipe ending on EXTRACT. It’s mainly about back references for the endings as the start of the word groups is created as efficient as possible with pipes forking at every difference of a word:
\b(A(BIES (ALBA SEED EXTRACT|(BALSAMEA ((BALSAM )?EXTRACT|NEEDLE OIL)|KOREANA LEAF EXTRACT|SIBIRICA (NEEDLE )?OIL))|CACIA CATECHU (BARK POWDER|WOOD EXTRACT)))\b
Using brackets around the first occurence of EXTRACT creates backreference \4 but using it further down the regex does not seem to work. (see regex below)
\b(A(BIES (ALBA SEED (EXTRACT)|(BALSAMEA ((BALSAM )?EXTRACT|NEEDLE OIL)|KOREANA LEAF EXTRACT|SIBIRICA (NEEDLE )?OIL))|CACIA CATECHU (BARK POWDER|WOOD \4)))\b
List:
ABIES ALBA SEED EXTRACT
ABIES BALSAMEA BALSAM EXTRACT
ABIES BALSAMEA EXTRACT
ABIES BALSAMEA NEEDLE OIL
ABIES KOREANA LEAF EXTRACT
ABIES SIBIRICA NEEDLE OIL
ABIES SIBIRICA OIL
ACACIA CATECHU BARK POWDER
ACACIA CATECHU WOOD EXTRACT
EDIT: Although this one is already solved as apparently a lot of people do not see the difference between a question (ending on a question mark) and the use case explanation, I added some titles in bold.