服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java基础:正则表达式的简单应用实例

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();
        }
    }
}

(t006)


扫描关注微信公众号