1.java中 取一个以"<author"开始,</author>结尾,中间不包含子串“<author”的字符串,用正则表达式如何写?
   比方说字符串为:"<author first ><author second>fddsa-authorddddfehauthor</author>".
  期待结果为"<author second>fddsa-authorddddfehauthor</author>".
  希望大大们帮忙下。谢谢了

解决方案 »

  1.   


    public static void main(String[] args) {
    String str="<author first > <author first ><author first ><author first ><author second>fddsa-authorddddfehauthor </author>";
    Pattern pattern = Pattern.compile("(<author [^>]*>[^<]+</author>)",Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
    Matcher m = pattern.matcher(str);
    while(m.find()) {
    System.out.println(m.group(1));
    }
    }
      

  2.   

    万一我的字符串是<author > <author second><dd ></author>". 
    能匹配出“<author second><dd ></author”吗?
      

  3.   

    个人一点偷懒的小建议,能不能不去试图直接操作你的select标签。因为那样通过寻找id的形式定位的确很麻烦。如果可以,尝试在select的外面嵌套一层div层,然后给div层一个id。这样display属性你就直接控制div层,可好
      

  4.   


    //按楼主要求,改过的,
    public static void main(String[] args) {
    String str="<author first1 > <author first2 ><author first3 ><bbb><aaa>fddsa-authorddddfehauthor </author>";
    Pattern pattern = Pattern.compile("(<author [^>]*>(?=<[^author][^>]*>).*?</author>)",Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
    Matcher m = pattern.matcher(str);
    while(m.find()) {
    System.out.println(m.group(1));
    }
    }
      

  5.   

    输出结果为:<author first3 ><bbb><aaa>fddsa-authorddddfehauthor </author>
      

  6.   

    to rascalboy520 :
    [^author]匹配的是 不包括author字符串呢还是不包括a,u,t,h,o,r任意一个字符??
      

  7.   

    前者,[^author]匹配的是 不包括author字符串
      

  8.   

    to rascalboy520:
    你说 [^author]匹配的是 不包括author字符串,
    实际上不是这样的,匹配的是不包括a,u,t,h,o,r任意一个字符
      

  9.   


    想匹配a,u,t,h,o,r任意一个字符
    用的应该是:String regex = "[^a|u|t|h|o|r]";
      

  10.   

    sorry,说错,
    想匹配a,u,t,h,o,r任意一个字符,应该是:String regex = "[a|u|t|h|o|r]";
    String regex = "[^a|u|t|h|o|r]"; 匹配的是不包括a,u,t,h,o,r任意一个字符 
      

  11.   

    to rascalboy520: 、
    我看了下参考文档,文档上说的是
    [^abc]    匹配 "a","b","c" 之外的任意一个字符,而不是abc字符串
    http://www.regexlab.com/zh/regref.htm
     
      

  12.   

    说了,我只是了解一点点哦,呵呵,还好你去看了,不然被我误导了,呵呵.
    [xyz] 字符集合。匹配所包含的任意一个字符。例如, '[abc]' 可以匹配 "plain" 中的 'a'。  
    [^xyz] 负值字符集合。匹配未包含的任意字符。例如, '[^abc]' 可以匹配 "plain" 中的'p'。 
    我仔细看了一下文档,帮你改了一下,之前写的有问题,public static void main(String[] args) {
    String str="<author first1 ><author first2 ><author first3 ><aaa><bbb>fddsa-authorddddfehauthor </author>";
            String regex = "(<author[^>]*>(?!<author).*?</author>)";
    Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
            Matcher m = pattern.matcher(str);
            while(m.find()) {
                System.out.println(m.group(1));
            }