In followup to my previous question Hundreds of RegEx on one string I ended up with a regex like following
(section1:|section2:|section3:|section[s]?4:|(special section:|it has:|synonyms:)).*?(?=section1:|section2:|section3:|section[s]?4:|(special section:|it has:|synonyms:)|$)
section section in regex search
The regex that I have in my prod system has more then 1000 characters and is multiple lines long. All it does is chunking sections from big piece of text and then again these sections are individually processed to extract information. Also I want these section titles to be natural language tolerant that's w开发者_运维百科hy some sections can be typed in multiple ways resulting in increased size of the regex. Is there a better way of doing this in terms of performance and manageability?
Use a lexical analyzer instead of regex.
For dealing with performance in such regexp you can use prefix optimisation https://code.google.com/p/graph-expression/wiki/RegexpOptimization
This framework allow you to write typechecked regexp with Java DSL. So it became refactorable and maintainable. https://code.google.com/p/graph-expression/
Maybe try a parser generator like one of those discussed in What's better, ANTLR or JavaCC? ?
If you have a natural language grammar then you typically have repeated sub-grammars to allow reordering. A proper grammar for that is going to be much easier to maintain than a regular expression.
精彩评论