Helen Araya
Helen Araya

Reputation: 1946

Java Regex Matcher not giving expected result

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.

Upvotes: 0

Views: 294

Answers (2)

Pshemo
Pshemo

Reputation: 124275

Problem with your regex is that . will not match line separators like \r or \n so your regex will stop before \r and since last part of your regex

(.*)((\n\n)|(\n)|(.))
     ^^^^^^^^^^^^^^^

is required and it can't match \r last character will be stored in (.).

If you don't want to include these line separators in your match just use "(.*)$"; pattern with Pattern.MULTILINE flag to make $ match end of each line (it will represent standard line separators like \r or \r\n or \n but will not include them in match).

So try with

String _partsPattern = "(.*)$"; //parenthesis are not required now
final Pattern partsPattern = Pattern.compile(_partsPattern,Pattern.MULTILINE);

Other approach would be changing your regex to something like (.*)((\r\n)|(\n)|(.)) or (.*)((\r?\n)|(.)) but I am not sure what would be the purpose of last (.) (I would probably remove it). It is just variation of your original regex.

Upvotes: 2

Joop Eggen
Joop Eggen

Reputation: 109613

Works, giving "PART1: 01/02/03 ". So my guess is that in the real code you read the text maybe with a Reader.readLine and erroneously strip a carriage return + linefeed. Far fetched but I cannot imagine otherwise. (readLine strips the newline itself.)

Upvotes: 0

Related Questions