字符串strConfig,形如eric;cole;bean
字符串strName为eric
如何判断strName是否在strConfig中注:只有全词匹配才行,例如当strName为ic或ol时,要匹配失败

解决方案 »

  1.   

    strConfig.contain(strName); 不可以?
      

  2.   


    Pattern  p = Patter.compile("eric");
    Matcher m = p .....m.match(strConfig);
      

  3.   

    这个比较简单,不需要用到正则,如:if(strConfig.indexOf(strName+";")==0)
      

  4.   

    如果一定要用正则,表达式如下:String regl = "$" + strName + ";";
      

  5.   


    String strConfig="eric;cole;bean";
    String strName = "eric";
    Pattern p = Pattern.compile(strName);
    Matcher m = p.matcher(strConfig);
    if(m.find()){
    System.out.println(m.group());
    }
      

  6.   

    String regex = "(^(eric);|;(eric);|;(eric)$)";这个是全词匹配 
      

  7.   

    我按照你给的方法试了 
    strConfig = eric;cole;gole
    匹配eric结果:true
    匹配ic结果:true
    匹配e结果:true
    匹配ole结果:true请对自己给出的答案负责好么
      

  8.   


    这个怎么用哇
    strConfig.matches(regex)  返回的是false
    是不是我用的方法错了
      

  9.   

    String regex = "(^(bean);|;(bean);|;(bean)$)";
    String strConfig="eric;cole;bean";
    Pattern p =Pattern.compile(regex);
    Matcher m=p.matcher(strConfig);
    while(m.find()){
      String s = m.group(0);
      System.out.println(s);
    }
      

  10.   


    String strConfig="eric;cole;bean";
            String strName = "eric";
            Pattern p = Pattern.compile(strName+";");
            Matcher m = p.matcher(strConfig);
            if(m.find()){
                System.out.println("true");
            }else{
                System.out.println("false");
            }
      

  11.   

    不用正则
    String strConfig="eric;cole;bean";
            String strName = "eric";        String[] aa = strConfig.split(";");
            for(String s : aa){
             if(strName.equals(s)){
                    System.out.println("true");
             }
            }
    或者        这个比较简单,不需要用到正则,如:if(strConfig.indexOf(strName+";")!=-1){
    true;}
      

  12.   


    哦抱歉,弄错了一个符号,$换成^就可以了 String strConfig = "eric;cole;bean";
    String strName="eric";
    String regl = "^" + strName + ";";
    Matcher m = Pattern.compile(regl).matcher(strConfig);
    System.out.println(m.find());
      

  13.   


    多谢 可以用了~~~请问为什么用Pattern.matches(regex, strConfig)和strConfig.matches(regex) 就会返回一个false
      

  14.   


    估计你没试过strName = "bean";
      

  15.   


    因为matches()是整体匹配,即strConfig整个字符串完全匹配这个正则表达式
    而find()是包含匹配,即只要在strConfig中发现有部分匹配就可以了
      

  16.   


    好像你说的很对
    api中 说 
    Pattern p = Pattern.compile("a*b");
     Matcher m = p.matcher("aaaaab");
     boolean b = m.matches();
    可以被
    boolean b = Pattern.matches("a*b", "aaaaab");
    代替 
    于是我想当然的就任认为所有情况下 都能代替
    文学功底不够
      

  17.   

    一定要用正则吗?这样可以不?
    String s = "eric;cole;bean";
    String s1 = "cole"; //s1 = co
    if (Arrays.binarySearch(s.split(";"), s1) >= 0) {
        System.out.println("yes");
    }
            
    if (Arrays.asList(s.split(";")).contains(s1)) {
        System.out.println("yes");
      

  18.   


    这个正则有问题的,只能匹配 eric,其他的,如 cole 匹配不了
      

  19.   

    而且如果只想匹配 eric
    那么 
    String s = "eric;cole;bean";
    String s1 = "eric";
    if (s.startsWith(s1+";")) 就可以了