Having this data file:
Test 1 Test 2 Test 3 Test 11
I would like to have a program to match each 'Test (.)', so I will get the output:
Match: 1 Match: 2 Match: 3 Match: 11
The program below gives this output:
Match: 1
The program is:
void addMysteries( Path path, String output) {
Pattern p = Pattern.compile(".?Test (.)");
try (Stream<String> lines = Files.lines( path)) {
lines.map( p::matcher)
.filter(Matcher::matches)
.forEach( matcher -> System.out.println("Match: " + matcher.group(1)));
} catch( Exception e) {
e.printStackTrace();
}
}
In Java 7, the software below is working fine. Why is this working and the above not?
pattern = Pattern.compile(".?Test (.)");
input = "Test 1\n\rOtherStuff\nTest 2 Test 3";
matcher = pattern.matcher(input);
while (matcher.find()) {
System.out.println( "Match: " + matcher.group( 1));
}