<tr>
<th>关键词(Tag):</th>
<td><input id="tag" name="tag" type="text">单个tag不多于12个字符,多个关键字请用空格分隔,最多填写5个。</td>
</tr>
上面是我前台form中的代码,参数传到java中后,用正则表达式如何判断啊?

解决方案 »

  1.   

    试试看这个:
    boolean isLegel = str.matches("\\S{1,12}(\\s\\S{1,12}){0,4}");
      

  2.   

    想起来对多个空格的情况处理不太谨慎,建议改为:
      boolean isLegel = str.matches("\\S{1,12}(\\s+\\S{1,12}){0,4}\\s*");
      

  3.   

    如果判断通过后,我要将这些tag提取出来该如何写
      

  4.   

    用 "\\S{1,12}" 作为正则,用Matcher进行循环匹配。
      

  5.   


    这个Google下就有很多样例吧
    String str = "内容";
    Pattern p = Pattern.compile("\\S{1,12}");
    Matcher m = p.matcher(str);
    while (m.find()) {
                System.out.println(m.group());
    }
      

  6.   

    ^([0-9a-zA-Z]{1,12} ){0,4}([0-9a-zA-Z]{1,12}){0,1}$
    那个[0-9a-zA-Z]可以写成/w
    你试试吧。昨天刚学的,试着写了一下。
      

  7.   

    另外如果取出各个值的话用String[] str
    str[i] = tag.split(" ")可以么?