package exams.july2017; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.regex.Matcher; import java.util.regex.Pattern; public class P03_RegexMon { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String input = reader.readLine(); String regexDidimon = "([^A-Za-z\\-]+)"; String regexBojomon = "([A-Za-z]+)\\-([A-Za-z]+)"; String regexDashes = "(\\-+)"; Pattern didimonPattern = Pattern.compile(regexDidimon); Matcher didimonMatcher = didimonPattern.matcher(input); Pattern bojomonPattern = Pattern.compile(regexBojomon); Matcher bojomonMatcher = bojomonPattern.matcher(input); Pattern dashesPattern = Pattern.compile(regexDashes); Matcher dashesMatcher = dashesPattern.matcher(input); String currentLine = input; String strToRemove = ""; while (true) { if (currentLine.length() == 0) break; if (didimonMatcher.find()) { System.out.println(didimonMatcher.group()); strToRemove = didimonMatcher.group(); currentLine = currentLine.substring(strToRemove.length(), currentLine.length()); } if (bojomonMatcher.find()) { System.out.println(bojomonMatcher.group()); strToRemove = bojomonMatcher.group(); currentLine = currentLine.substring(strToRemove.length(), currentLine.length()); } if (dashesMatcher.find()) { strToRemove = dashesMatcher.group(); currentLine = currentLine.substring(strToRemove.length(), currentLine.length()); } } } }