import java.util.regex.Matcherimport java.util.regex.Patternpublic class E { public static void main(String[] args) { Pattern p = Pattern.compile("[A-Za-z]+")//设定匹配规则为取出字符串中的字母 Matcher m = p.matcher("12sifiwq820aufu")//与字符串匹配 while(m.find()){System.out.println(m.group()) } }}运行结果:
sifiwqaufu
这个问题不用正则表达式,用JavaString类的contains函数就可以解决了。
具体的Java程序代码如下:
public class CB {public static void check(String source,String target){
boolean flag=false
int i
for(i=0i<source.length()i++){
if(!target.contains(source.charAt(i)+"")){
break
}
}
if(i==source.length()) flag=true
if(flag==true){
System.out.println(source+"和"+target+"匹配")
}else{
System.out.println(source+"和"+target+"不匹配")
}
}
public static void main(String[] args) {
check("6482","600000")
check("6482","006400")
check("6482","020864")
}
}
运行结果:
6482和600000不匹配
6482和006400不匹配
6482和020864匹配
java 正则匹配字符为纯数字方法:定义正则表达式为:
String reg="^\\d+$"
获取要判断的字符串:
String str;//可以通过Scanner从控制台输入,也可以用字符串常量进行初始化
调用字符串的matches方法判断字符串为纯数字情况:
str.matches(reg);
如果是纯数字返回为true,否则返回为false;