正则表达式的简单应用实例

import java.io.*; import java.util.regex.*; public class Printer { public static void main(String[] args) { System.out.println(" Please enter the input string: "); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String inputString; boolean isOK = false; try { while(!isOK) { if((inputString = reader.readLine()) != null) { if(inputString.length() > 200) { System.out.println("The string exceeds 200 characters. Please enter again! "); } else { Pattern regex = Pattern.compile("[^@#$%&*/^]+"); Matcher matcher = regex.matcher(inputString); boolean isMatched = matcher.matches(); if(!isMatched) { System.out.println("The String cant contain @,#,$,%,*,& and ^. Please enter again! "); } else { isOK = true; System.out.println(" Your input string is: " + inputString); } } } } } catch(IOException e) { e.printStackTrace(); } } } <淘宝热门商品:
 

16.00 元  

减肥极品魔芋胶

 

保健品/滋补品 

淑芳阁_苗条姿_最有效的减肥瘦身与丰胸专卖店

来源:程序员网

小小豆叮

0 Responses to "正则表达式的简单应用实例"

发表评论