public class P296Pattern {
public static void main(String[] args) {
Pattern x = Pattern.compile("\\w");
Matcher m = x.matcher("aasdfasdf adsfasdf23");
while(m.find()){
System.out.println(m.start()+ " ");
}
}
}
public static void main(String[] args) {
Pattern x = Pattern.compile("\\w");
Matcher m = x.matcher("aasdfasdf adsfasdf23");
while(m.find()){
System.out.println(m.start()+ " ");
}
}
}
是想找出字母??[a-zA-Z]
import java.util.regex.*;public class P296Pattern {
public static void main(String[] args) {
String str = "aasdfasdf adsfasdf23";
Pattern x = Pattern.compile("\\w");
Matcher m = x.matcher(str);
while(m.find()){
System.out.println(str.charAt(m.start())+ " ");//这样就好了,m.start返回的就是匹配
// 成功的索引
}
}
}建议多看API文档!
public static void main(String[] args) {
String str = "aasdfasdf adsfasdf23";
Pattern x = Pattern.compile("\\w");
Matcher m = x.matcher(str);
while(m.find()){
System.out.println(m.group());
}
}
}ls的结果应该和这个是一样的,每行输出每个英文/数字字符
你试试这个是不是你要的
public static void main(String[] args) {
String str = "aasdfasdf adsfasdf23";
Pattern x = Pattern.compile("[a-z|A-Z]");
Matcher m = x.matcher(str);
while(m.find()){
System.out.print(m.group()); //the result :aasdfasdfadsfasdf
}
}
其次m.group()才是得到匹配的结果。而m.start得到的当次匹配成功的开始位置。你也可以通过m.end得到匹配成功的结束位置。
LZ 应该说清楚点,我想 m.group() 和 m.start()的区别应该知道吧
这个单个字符的分组 m.group() 就行了吧,不用那么麻烦吧