├── Java Regex └── Java Regex2 /Java Regex: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.regex.Matcher; 3 | import java.util.regex.Pattern; 4 | class Solution{ 5 | public static void main(String[] args){ 6 | Scanner in = new Scanner(System.in); 7 | while(in.hasNext()){ 8 | String IP = in.next(); 9 | System.out.println(IP.matches(new MyRegex().pattern)); 10 | } 11 | } 12 | } 13 | class MyRegex{ 14 | String num = "([01]?\\d{1,2}|2[0-4]\\d|25[0-5])"; 15 | String pattern = num + "." + num + "." + num + "." + num; 16 | } 17 | -------------------------------------------------------------------------------- /Java Regex2: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.regex.Matcher; 3 | import java.util.regex.Pattern; 4 | public class DuplicateWords { 5 | public static void main(String[] args) { 6 | String regex = "\\b(\\w+)(?:\\W+\\1\\b)+"; 7 | Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); 8 | Scanner in = new Scanner(System.in); 9 | int numSentences = Integer.parseInt(in.nextLine()); 10 | while (numSentences-- > 0) { 11 | String input = in.nextLine(); 12 | Matcher m = p.matcher(input); 13 | while (m.find()) { 14 | input = input.replaceAll(m.group(), m.group(1)); 15 | } 16 | System.out.println(input); 17 | } 18 | in.close(); 19 | } 20 | } 21 | --------------------------------------------------------------------------------