• Java


    验证

    简单验证

    String regex = "\d{4}-\d{2}-\d{2}";
    String input = "2016-01-01";
    assertTrue(input.matches(regex));
    assertTrue(Pattern.matches(regex, input));

    提取

    String regex = "\d{4}-\d{2}-\d{2}";
    String input = "2016-01-01, 2016-02-02. [2016-03-03]";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(input);
    while (matcher.find()) {
        System.out.println(matcher.group());
    }

    替换

    简单替换

    String regex = "(\d{4})-(\d{2})-(\d{2})";
    String replacement = "$2/$3/$1";
    String input = "2016-01-15, 2016-02-15.";
    String actual = input.replaceAll(regex, replacement);
    String expected = "01/15/2016, 02/15/2016.";
    assertEquals(expected, actual);

    使用 Pattern 对象,方便反复使用

    String regex = "(\d{4})-(\d{2})-(\d{2})";
    String replacement = "$2/$3/$1";
    String input = "2016-01-15, 2016-02-15.";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(input);
    String actual = matcher.replaceAll(replacement);
    String expected = "01/15/2016, 02/15/2016.";
    assertEquals(expected, actual);

    切分

    简单切分

    String regex = "\s+";
    String input = "a  b	c";
    String[] actuals = input.split(regex);
    String[] expecteds = new String[] {"a", "b", "c"};
    assertArrayEquals(expecteds, actuals);

    使用 Pattern 对象,方便反复使用

    String regex = "\s+";
    String input = "a  b	c";
    Pattern pattern = Pattern.compile(regex);
    String[] actuals = pattern.split(input);
    String[] expecteds = new String[] {"a", "b", "c"};
    assertArrayEquals(expecteds, actuals);
  • 相关阅读:
    test deploy
    SpringBoot+Spring Session+Redis实现Session共享及踩坑记录
    登录接口取cookie
    pycharm设置自动调节字体大小
    ClickHouse高可用集群
    clickhouse 常用命令(三)
    clickHouse常用命令(一)
    ClickHouse表引擎
    clickhouse基本数据类型
    手写滚动条设计----直接粘贴
  • 原文地址:https://www.cnblogs.com/huey/p/5531065.html
Copyright © 2020-2023  润新知