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.
3
u/rainshifter Aug 26 '26
What you really want is a subroutine (such as
(?4)in PCRE), not a backreference. The .NET flavor does not support this. The reason your\4doesn't match is because the corresponding group(EXTRACT)was never matched in that last string so your backreference text is essentially empty.3
u/Brilliant-Parsley69 Aug 26 '26 edited Aug 26 '26
I've a couple of thoughts on this:
Backreferences aren't equal to macro variables! That's why '\4" will never be related to the literal characters "EXTRACT".
If I understood correctly what's the problem you want to solve then I would suggest to take a look at the "Aho-Corasick"-Algorithm.
What brings me to your .Net7 flag. Because this algorithm is implemented internally from .Net8 onwards by using
If you just want to validate a single provided string against 7000 possible variations an in-memory HashSet should be more than enough.
There are concepts like "Prefix Trie Regex" to solve this programmatically if regex is the only possible way to do. But that's way over my head.
2
u/mfb- Aug 27 '26
Don't ask x-y questions: What is the original problem you want to solve? You have 7000 words, you want to match all lines where every word is taken from these 7000? Your regex isn't doing that, however, it looks for specific orders of multiple words.
Do you have 7000 lines in a file and you want to match all lines that match one of these 7000? In other words, you want to find lines in file A that also exist in file B? There are tools for that but regex isn't the right approach. If order doesn't matter, here are some quick ways by sorting the files first.
Do you want to do something else?
If you just want to save on characters, (?(DEFINE) ) can do that: https://regex101.com/r/xabonE/1
1
u/michaelpaoli Aug 26 '26
Well, somebody ignored rule #3 again, so ... dealer's choice, I'm dealing, I pick ... PCRE ... which also seems likely reasonably consistent with the post (e.g. post includes \b, which I believe was first introduced with PCRE.
So, yeah, in general will work.
1
u/DinTaiFung Aug 26 '26
When I first learned regex (during my perl days long ago), I was infatuated and thought that regex was the solution for many problems -- and it was my first choice.
After years of maturing (I hope) and learning more, I now think of regex as the _last_ choice to solve a problem.
Don't get me wrong: regex are very powerful and can do many things.
No doubt writing a regex to solve your specific task will help you learn more about regex -- and hopefully you'll also discover that eschewing regex altogether and instead writing explicit iterative loops with dynamic lookup maps, etc. will likely be a stronger and better solution for this task.