import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
  public static void main(String[] argv) throws Exception {
    Matcher m = Pattern.compile("(?:^|(?<=\\s))(?=(\\S+\\s+\\S+)(?=\\s|$))")
      .matcher("word1 word2 word3 word4");
    while (m.find()) {
      System.out.println(m.group(1));
    }
  }
}