public static void main(String []args){
String str="abc";
Pattern pattern = Pattern.compile("[a-z]");
Matcher matcher = pattern.matcher(str);
if(matcher.matches()){
//为什么总匹配不上呢?
}
}
这里为什么是总匹配不上呢??汗
String str="abc";
Pattern pattern = Pattern.compile("[a-z]");
Matcher matcher = pattern.matcher(str);
if(matcher.matches()){
//为什么总匹配不上呢?
}
}
这里为什么是总匹配不上呢??汗
String str="abc";
Pattern pattern = Pattern.compile("[a-z]");
Matcher matcher = pattern.matcher(str);
if(matcher.matches()){ 你的这个只是匹配 a~z 之间的一个字母
换成这样
Pattern pattern = Pattern.compile("[a-z]*");
String str="abc";
Pattern pattern = Pattern.compile("[a-z]");
Matcher matcher = pattern.matcher(str);
if(matcher.find()){
System.out.println(matcher.group());
//为什么总匹配不上呢?
}
}
[a-z]*
[a-z]+
[a-z]{3}
[a-z]{3,}
[a-z]{0,3}
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class Test { /**
*
* @param args
*/
public static void main(String[] args) {
String str = "abc";
Pattern pattern = Pattern.compile("[a-z]", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(str);
for (int i = 0; i < str.length(); i++) {
if (matcher.find()) {
System.out.println(matcher.group());
}
}
}}/**结果a
b
c
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class Test { /**
*
* @param args
*/
public static void main(String[] args) {
String str = "abc";
Pattern pattern = Pattern.compile("[a-z]");
Matcher matcher = pattern.matcher(str);
for (int i = 0; i < str.length(); i++) {
if (matcher.find()) {
System.out.println(matcher.group());
}
}
}}
------------------------------------
String str="abc";
Pattern pattern = Pattern.compile("[a-z]");
Matcher matcher = pattern.matcher(str);
while(matcher.find()){
System.out.println(matcher.group());
}
Matcher matcher = pattern.matcher(str);
if(matcher.matches()){
其中matches方法是拿整个字符串和正则表达式比,而[a-z]代表的是a-z的一个字符;肯定不行