正则:<(.*?)>(.*)
写个循环提取。每次提取完group(1),再用group(2)内容继续与正则匹

解决方案 »

  1.   


                    String value = "<YLPY-F><XLPY-F><PY-3><RHP-6><SDY-1><LXBY-6><ZBL-P810>";
    Pattern pattern = Pattern.compile("<(.*?)>");
    Matcher matcher = pattern.matcher(value);
    while(matcher.find()){
    System.out.println(matcher.group(1));
    }
      

  2.   


    public static void main(String[] args) {
            String str = "<YLPY-F><XLPY-F><PY-3><RHP-6><SDY-1><LXBY-6><ZBL-P810>";
            String arr[] = str.split("[><]+");
            for(int i=1;i<arr.length;i++){
                System.out.println(arr[i]);
            }
        }
      

  3.   

    String value = "<YLPY-F><XLPY-F><PY-3><RHP-6><SDY-1><LXBY-6><ZBL-P810>";
            Pattern pattern = Pattern.compile("(<(.*?)>)");
            Matcher matcher = pattern.matcher(value);
            while(matcher.find()){
                System.out.println(matcher.group(2));
            }
      

  4.   

    for example
    String s = "<YLPY-F><XLPY-F><PY-3><RHP-6><SDY-1><LXBY-6><ZBL-P810>";
    Pattern p = Pattern.compile("<(.*?)>");
    Matcher m = p.matcher(s);
    while (m.find()) {
        System.out.println(m.group(1));
    }
      

  5.   

    String s = "<<YLPY-F>bbb<aaa>><XLPY-F><PY-3><RHP-6><SDY-1><LXBY-6><ZBL-P810>";
    Pattern p = Pattern.compile("(?<=[<>])([^<>].*?)(?=[<>])");
    Matcher m = p.matcher(s);
    while (m.find()) {
        System.out.println(m.group(1));
    }
    是这个意思吗? <<YLPY-F>bbb<aaa>> 的YLPY-F和bbb和aaa都取出来?
      

  6.   


    String str = "<XLPY-F><PY-3><RHP-6><SDY-1><LXBY-6><ZBL-P810>";可能后面还有<LXBY-6><ZBL-P810>..不晓得有多少这种<XX>;
    现在就是想把所有尖括号内的内容取出来
    如果用group(1)只能取出第一个,如果再用group(2)去比对,那我不知到要比对多少次
      

  7.   


     string str = "<YLPY-F><XLPY-F><PY-3><RHP-6><SDY-1><LXBY-6><ZBL-P810>";
                foreach(Match macth in Regex.Matches(str, @"<(.+?)>"))
                {
                    if ( macth.Success)
                    {
                        Console.WriteLine("Found '{0}' at position {1} ,Group is   {2} ",
                               macth.Value, macth.Index, macth.Groups[1]);
                    }
                }