利用 正则表达式 获取两个字符串中间的值

直接上代码吧,不是很难。

    public static void main(String[] args) {

        // 内容
        String value = "fileNameCode-->_AD2467524284sd234.json";

        // 匹配规则
        String reg = "_(.*?)\\.";
        Pattern pattern = Pattern.compile(reg);

        // 内容 与 匹配规则 的测试
        Matcher matcher = pattern.matcher(value);

        if( matcher.find() ){
            // 包含前后的两个字符 
            System.out.println(matcher.group());
            // 不包含前后的两个字符
            System.out.println( matcher.group(1) );
        }else{
            System.out.println(" 没有匹配到内容....");
        }
    }

 

注意:这个 . 需要:\\. 这样。

() --> 标记 一个子表达式 开始 和 结束 的位置。

. --> 匹配除换行符 \n 之外的任何单字符。

* --> 匹配前面的子表达式零次或者多次。

? --> 匹配前端的子表达式零次或者一次。

 

相关文章:

  • 2021-11-20
  • 2021-12-18
  • 2021-11-23
  • 2021-11-22
  • 2022-12-23
  • 2022-12-23
  • 2021-09-01
猜你喜欢
  • 2021-11-26
  • 2022-02-07
  • 2022-02-09
  • 2022-01-31
相关资源
相似解决方案