首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

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

2012-01-29 
用正则表达式匹配非0且非114我想用正则表达式匹配一个非0且非114的数字,请高手指点这个正则表达式怎么写。J

用正则表达式匹配非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());      } 

热点排行