Including comments in Java regular expressions

后端 未结 3 1373
借酒劲吻你
借酒劲吻你 2021-01-04 04:41

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 w

相关标签:
3条回答
  • 2021-01-04 05:28

    I found the following worked:

            String pattern2S = 
                "([A-Za-z]+)      # mandatory firstName\n" +
                "\\s+             # mandatory whitespace\n " +
                "([A-Z]\\.)?      # optional initial\n" +
                "\\s+             # whitespace\n " +
                "([A-Za-z]+)      # mandatory lastName\n"; 
    

    The key thing was to include the newline character \n explicitly in the string

    0 讨论(0)
  • 2021-01-04 05:30

    Why don't you just do this:

    String pattern2S = 
        "([A-Za-z]+)" + //    mandatory firstName
        "\\s+" +        //    mandatory whitespace
        ...;
    

    CONTINUATION:

    If you want to keep the comments with the pattern and you need to read it in from a properties file, use this:

    pattern=\
    #comment1\\n\
    (A-z)\
    #comment2\\n\
    (0-9)
    
    0 讨论(0)
  • 2021-01-04 05:34

    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;
    
    0 讨论(0)
提交回复
热议问题