正则表达式判断质数(素数)的代码

看到一篇关于正则表达式妙用的文摘——《检查素数的正则表达式》,正则玩到了极致(反向引用+非贪婪模式),不错的思路,可以借鉴。原文:http://coolshell.cn/articles/2704.html


java实现了一个demo,仅供研究参考:


 

public class PatternMatchesPrimeNumber {
 public static void main1(String[] args) {
 String regex="^1?$|^(11+?)\\1+$";
 StringBuffer sb=new StringBuffer();
 for(int i=1;i<1000;i++){
 sb.append("1");
 if(!sb.toString().matches(regex)){
 System.out.println(i);
 }
 }
 }
}


 

 

posted @ 2013-05-07 19:37  javawebsoa  Views(263)  Comments(0Edit  收藏  举报