zl程序教程

您现在的位置是:首页 >  后端

当前栏目

java正则,将<a或者</a,尖括号后面的字母改成大写

JAVA lt 或者 正则 字母 大写 后面 改成
2023-09-14 08:57:51 时间

java正则,将<a或者</a,尖括号后面的字母改成大写

/**
     * 将<a或者</a中的a,转为大写字母
     * @param xmlStr
     * @return
     */
    public static String firstLabelToUppper(String xmlStr){
        Pattern p = Pattern.compile("\\<[a-z|A-Z]");
        Matcher m = p.matcher(xmlStr);
        StringBuffer sb = new StringBuffer();
        while (m.find())
        { // Find each match in turn; String can't do this.
//String name = m.group(1); // Access a submatch group; String can't do this.
            m.appendReplacement(sb,  m.group().toUpperCase());
           // System.out.println("m.group() is= " + m.group());
        }
        m.appendTail(sb);
        //System.out.println("sb is= " + sb);

        return lastLabelToUppper(sb.toString());
    }

    /**
     * 将<a或者</a中的a,转为大写字母
     * @param xmlStr
     * @return
     */
    public static String lastLabelToUppper(String xmlStr){
        Pattern p = Pattern.compile("\\</[a-z|A-Z]");
        Matcher m = p.matcher(xmlStr);
        StringBuffer sb = new StringBuffer();
        while (m.find())
        { // Find each match in turn; String can't do this.
//String name = m.group(1); // Access a submatch group; String can't do this.
            m.appendReplacement(sb,  m.group().toUpperCase());
            //System.out.println("m.group() is= " + m.group());
        }
        m.appendTail(sb);
        //System.out.println("sb is= " + sb);

        return sb.toString();
    }