一个字符串上有数百个正则表达式
跟进我之前的问题 一个字符串上有数百个正则表达式 我最终得到了一个正则表达式,如下
(section1:|section2:|section3:|section[s]?4:|(special section:|it has:|synonyms:)).*?(?=section1:|section2:|section3:|section[s]?4:|(special section:|it has:|synonyms:)|$)
我的正则表达式prod 系统有超过 1000 个字符并且有多行长。它所做的就是从大段文本中分块,然后再次对这些部分进行单独处理以提取信息。另外,我希望这些部分标题能够兼容自然语言,这就是为什么某些部分可以通过多种方式键入,从而导致正则表达式的大小增加。在性能和可管理性方面有更好的方法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用词法分析器而不是正则表达式。
Use a lexical analyzer instead of regex.
为了处理此类正则表达式中的性能,您可以使用前缀优化https:// code.google.com/p/graph-expression/wiki/RegexpOptimization
此框架允许您使用 Java DSL 编写类型检查的正则表达式。所以它变得可重构和可维护。 https://code.google.com/p/graph-expression/
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/
也许尝试一下解析器生成器,就像 ANTLR 或 JavaCC 哪个更好? 中讨论的那样。 ?
如果您有自然语言语法,那么通常会有重复的子语法以允许重新排序。正确的语法比正则表达式更容易维护。
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.