【Java】【正则表达式】正则表达式

【Java】【正则表达式】正则表达式_第1张图片

package _09_regex;

import java.util.Scanner;
import java.util.regex.Pattern;
import com.easing.commons.util.format.TextUtil;
import lombok.Cleanup;

public class _01_Regex
{
  public static void main(String[] args)
  {
    // f1();
    // f2();
    f3();
  }
  
  // 校验手机号格式
  private static void f1()
  {
    @Cleanup
    Scanner scanner = new Scanner(System.in);
    String input = scanner.nextLine();
    
    String regex = "^1[3,8]\\d-\\d{4}-[0-9]{4}$";
    
    if (input.matches(regex))
      System.out.println("match ok");
    else
      System.out.println("match fail");
  }
  
  // 校验IP格式
  private static void f2()
  {
    @Cleanup
    Scanner scanner = new Scanner(System.in);
    String input = scanner.nextLine();
    
    String regex1 = "^(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|[1-9])\\.";
    String regex2 = "(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.";
    String regex3 = "(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.";
    String regex4 = "(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)$";
    String regex = regex1 + regex2 + regex3 + regex4;
    
    if (input.matches(regex))
      System.out.println("match ok");
    else
      System.out.println("match fail");
  }
  
  private static void f3()
  {
    @Cleanup
    Scanner scanner = new Scanner(System.in);
    String input = scanner.nextLine();
    
    // 判断输入是否匹配正则表达式
    String regex = "^(.*)$";
    boolean matches = Pattern.matches(regex, input);
    
    // 根据正则表达式分割字符串
    regex = "\\?|\\*";
    String[] splits = Pattern.compile(regex).split(input);
    
    System.out.println(matches);
    System.out.println(TextUtil.arrayToString1(splits));
  }
}

你可能感兴趣的:(java)