Sap
Sap

Reputation: 5291

Hundreds of RegEx on one string

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 why 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?

Upvotes: 2

Views: 233

Answers (3)

yura
yura

Reputation: 14645

  1. For dealing with performance in such regexp you can use prefix optimisation https://code.google.com/p/graph-expression/wiki/RegexpOptimization

  2. This framework allow you to write typechecked regexp with Java DSL. So it became refactorable and maintainable. https://code.google.com/p/graph-expression/

Upvotes: 1

Mike Samuel
Mike Samuel

Reputation: 120506

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.

Upvotes: 1

xpda
xpda

Reputation: 15813

Use a lexical analyzer instead of regex.

Upvotes: 4

Related Questions