在Java正则表达式中包含注释 [英] Including comments in Java regular expressions
问题描述
我有一些复杂的正则表达式,我需要评论的可读性和维护。 Java规范相当简洁,我很久以来一直在努力工作。我终于抓住了我的错误,并将它作为一个答案,但我会感谢任何其他建议维护正则表达式
I have some complex regular expressions which I need to comment for readability and maintenance. The Java spec is rather terse and I struggled for a long time getting this working. I finally caught my bug and will post it as an answer but I'd be grateful for any other advice on maintaining regexes
作为一个例子,我想评论子组件的一个简单的名称解析器:
As an example I want to comment the subcomponents (of patternS) in a simple name parser:
String testTarget = "Waldorf T. Flywheel";
String patternS = "([A-Za-z]+)\\s+([A-Z]\\.)?\\s+([A-Za-z]+)";
Pattern pattern = Pattern.compile(patternS, Pattern.COMMENTS);
Assert.assertTrue(pattern.matcher(testTarget).matches());
编辑:我还要感谢(?x)格式的例子。
I would be grateful for examples of the (?x) format as well.
编辑:@ geowa4有一个很好的建议,避免嵌入的评论。 Sinnce Java和其他人提供了嵌入式评论什么是他们有用的情况? (我想我有一个case,但我有兴趣看到别人)。
@geowa4 has a good suggestion which avoids embedded comments. Sinnce java and others have provided for embedded comments what are the cases where they are useful? (I think I have a case but I'd be interested to see others).
编辑:如下所示@mikej正则表达式不支持可选的初始井,如下:
As noted below @mikej the regex does not support the optional initial well and would be better as:
String patternS = "([A-Za-z]+)\\s+([A-Z]\\.\\s+)?([A-Za-z]+)";
但最终会提取最初的空格
but that would end up extracting space in the initial
推荐答案
请参阅 Martin Fowler 对 ComposedRegex 一些关于提高regexp可读性的想法。总之,他主张将一个复杂的regexp分解成更小的部分,可以给出有意义的变量名。例如
See the post by Martin Fowler on ComposedRegex for some more ideas on improving regexp readability. In summary, he advocates breaking down a complex regexp into smaller parts which can be given meaningful variable names. e.g.
String mandatoryName = "([A-Za-z]+)";
String mandatoryWhiteSpace = "\\s+";
String optionalInitial = "([A-Z]\\.)?";
String pattern = mandatoryName + mandatoryWhiteSpace + optionalInitial +
mandatoryWhiteSpace + mandatoryName;
这篇关于在Java正则表达式中包含注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!