I have the following code.
String _partsPattern = "(.*)((\n\n)|(\n)|(.))";
static final Pattern partsPattern = Pattern.compile(_partsPattern);
String text= "PART1: 01/02/03\r\nFindings:no smoking";
Matcher match = partsPattern.matcher(text);
while (match.find()) {
System.out.println( match.group(1));
return; //I just care on the first match for this purpose
}
Output: PART1: 01/02/0 I was expecting PART1: 01/02/03 why is the 3 at the end of my text not matching in my result.
(.)? That will match any character.PART1: 01/02/03and then (second match):Findings:no smokinwhile (match.find()) {butif (match.find()) {. This way you will remove unnecessaryreturnstatement. Also last part of your regex is(.)which in case there will be no line separators will hold last character from entire match, so this may be reason why instead ofPART1: 01/02/03you seePART1: 01/02/0-3may be in group(5).