java中的正则表达式问题:为什么匹配不到
if (strWord.matches( "[\ ",] ")
{
return true;
}
当其中strWord= " "mm,aa "的时候,为什么return false?
(strWord中有引号,逗号和字母)
------解决方案--------------------用String的matches匹配不到吧,可以用Matcher的find()方法
import java.util.regex.*;
public class Test
{
public static void main(String [] args)
{
Pattern p = Pattern.compile( "[\ ",] ");
Matcher m = p.matcher( "mm\ ",,nn ");
while(m.find())
{
System.out.println( "adf ");
}
}
}
------解决方案--------------------import java.util.regex.* ;
public class RegexTest {
public static void main(String[] args) {
String word= "mm,aa ";
System.out.println(strword(word));
}
public static boolean strword(String word){
Pattern p = Pattern.compile( "\\w*([\ "]{1}|[,]{1})\\w* ");
if(word!=null){
Matcher m = p.matcher(word);
return m.matches();
}else{
return true;
}
}
}