if (strWord.matches("[\",]")
{
    return true;
}当其中strWord=""mm,aa"的时候,为什么return false?
(strWord中有引号,逗号和字母)

解决方案 »

  1.   

    match相当于^$全部要匹配,中间包含不行的
      

  2.   

    那么我想让strWord中包含引号和逗号就返回true
    应该怎么做阿
      

  3.   

    JAVA API 上说 
    当且仅当整个区域序列匹配此匹配器的模式时才返回 true此方法调用的 str.matches(regex) 
    形式与以下表达式产生完全相同的结果: Pattern.matches(regex, str)Pattern.matches(regex, input);与表达式 
    Pattern.compile(regex).matcher(input).matches() 的行为完全相同。
      

  4.   

    我想让输入的strWord中包含引号或者逗号的情况下,返回true 这种情况用正则表达式不能做到吗?
      

  5.   

    我把源代码也贴一下
    private boolean InputCheck(String strWord) 

       if (strWord.matches("[\",]") { return true; } 
       return false; 
    }
      

  6.   

    函数的机能:check输入的字符串, 只要有引号或者逗号,就返回true 
    用正则表达式能实现吗?(不用for循环便利每一个字符的情况下)
      

  7.   

    用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");
        }
        
     }
    }
      

  8.   

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

    }
    }