zoukankan      html  css  js  c++  java
  • 请写出正则表达式(regex),取得下列黄色部分的字符串 TEL: 02-236-9655/9659 FAX:02-236-9654 (黄色部分即02-236-9655/9659 ) ( 测试面试题)

      请写出正则表达式(regex),取得下列黄色部分的字符串

    TEL: 02-236-9655/9659 FAX:02-236-9654

    答:   

    package test1;

    import java.util.regex.Matcher;

    import java.util.regex.Pattern;

    /**

     * Copyright (c) 2016年7月11日 Leon All rights reserved. 

     *

     * Description:    请写出正则表达式(regex),取得下列黄色部分的字符串    02-236-9655/9659

     * TEL: 02-236-9655/9659 FAX:02-236-9654

     */

    public class LeonDome {

       public static void main(String[] args) {

       //原来字符串

       String string = "TEL: 02-236-9655/9659 FAX:02-236-9654";

                

       //根据网上找到的改编        

       String regex = "(\d{1,2}\-\d{1,3}\-\d{1,4}\/\d{1,4})";

       

       //打印获取到的黄色字符串   

       System.out.println(getMatcher(regex,string));

    }

     /**

      * 取得下列黄色部分的字符串 :02-236-9655/9659

      * @param regex 正则表达式

      * @param source 字符串

      * @return 返回需要的字符串

      */

     public static String getMatcher(String regex, String source) {  

              String result = "";  

              Pattern pattern = Pattern.compile(regex);  

              Matcher matcher = pattern.matcher(source);  

              while (matcher.find()) {  

                  result = matcher.group(1);

              }  

              return result;  

    }  

    }

     Created by Leon on 2018/7/6. Copyright © Leon. All rights reserved.

  • 相关阅读:
    Matlab smooth函数原理
    Pandas中的高级索引loc、iloc、ix精简概括
    QT常见错误:"multiple definition of xxx"
    Github术语解释
    数据反转 LSB
    LSB最低有效位和MSB最高有效位
    Modbus通信CRC16校验程序
    CRC16常见几个标准的算法及C语言实现
    DB9 公头母头引脚定义及连接
    hdu 2577 How to Type(dp)
  • 原文地址:https://www.cnblogs.com/henanleon/p/11261108.html
Copyright © 2011-2022 走看看