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
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
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)
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;