I am programmatically generating a very lengthy regex expression in a lua script I'm developing. It works, but at 960 characters, I can't help thinking it can be simplified a bit. However, I can't think of a way to do that without breaking it. In a nutshell, it looks for a string of 1 or more space-separated predefined keywords. A much-shortened version of the regex looks like this. You can see the major portion of it, the keyword list, is repeated.
\(big\|hairy\|regex\)\( \+\(big\|hairy\|regex\)\)*
To remove the duplication, I could use this one. The problem here is that the string starts with a space; whereas, I need it to start with any of the keywords.
\( \+\(big\|hairy\|regex\)\)\+
Of course the next regex doesn't work because \1
refers to the text that was matched and not the regex that found it.
\(big\|hairy\|regex\)\( \+\1\)*
Can the regex be written so that the keyword list appears only once, but still matches correctly?
\%#=1
to the beginning to use the old regex engine. I'll revisit that when I try Vivian's suggestion.