编程语言

img aquariusmao

正则表达式在java中的一个简单应用实例

发表于2004/9/28 0:09:00  618人阅读

import java.io.*;
import java.util.regex.*;

public class Printer {
    public static void main(String[] args) {
        System.out.println("/nPlease enter the input string:/n");
        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./nPlease enter again!/n");
                    }
                    else {
                        Pattern regex = Pattern.compile("[^@#$%&*/^]+");
                        Matcher matcher = regex.matcher(inputString);
                        boolean isMatched = matcher.matches();
                        if(!isMatched) {
                            System.out.println("The String can't contain @,#,$,%,*,& and ^./nPlease enter again!/n");
                        }
                        else {
                            isOK = true;
                            System.out.println("/nYour input string is: /n" + inputString);
                        } 
                    }
                }
            }
        }
        catch(IOException e) {
            e.printStackTrace();
        }
    }
}

阅读全文
0 0

相关文章推荐

img
取 消
img