当前位置: 代码迷 >> Java相关 >> 用正则表达式匹配非0且非114,该怎么处理
  详细解决方案

用正则表达式匹配非0且非114,该怎么处理

热度:8413   发布时间:2013-02-25 21:50:13.0
用正则表达式匹配非0且非114
我想用正则表达式匹配一个非0且非114的数字,请高手指点这个正则表达式怎么写。
Java code
public class Test {    public static void main(String[] args) {        String str = "114";        System.out.println(str.matches("^(0|114)"));    }}

以上代码不能得到正确结果

------解决方案--------------------------------------------------------
Java code
    String regex = "^(" +            "[1-9]" + // 个            "|" +            "[1-9]\\d" + // 十            "|" +            "[1-9]\\d{3,}" + // 千以上            "|" +            "[2-9]\\d{2}" + // 2xx-9xx            "|" +            "1[02-9]\\d" + // 10x,12x-19x            "|" +            "11[0-35-9]" + // 110-113,115-119            ")$";    for (int i = 0; i < 10000; i++) {      String str = i+"";      if (!str.matches(regex) && i != 0 && i != 114) {        throw new Exception(str);      }    }
------解决方案--------------------------------------------------------
Java code
      String str = "0s14dfd22";      Pattern p = Pattern.compile("[^0,114]+");      Matcher m = p.matcher(str);      while(m.find()) {         System.err.println(m.group());      }