您当前的位置: 首页 >  Java

命运之手

暂无认证

  • 2浏览

    0关注

    747博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

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

命运之手 发布时间:2017-11-24 16:16:28 ,浏览量:2

这里写图片描述

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));
  }
}

关注
打赏
1654938663
查看更多评论
立即登录/注册

微信扫码登录

0.0762s