解析常用文本模板正则表达式

解析常用文本模板正则表达式

A:

    /**
     * Parse template with below:
     * @author jervalj
     * 
     * [tag1]
     * value1
     * value1
     * value1
     * 
     * [tag2]
     * value2
     * value2
     * 
     * @param textString
     */
    private void parse2Map(String textString) {
        if (null != textString) {
            Pattern pattern = Pattern.compile("\\[([^\\]]+)\\]([^\\[]*)", Pattern.DOTALL);
            Matcher matcher = pattern.matcher(textString);
            while (matcher.find()) {
                // group(1) is the 'tag',group(2) is 'value'
                templateMap.put(matcher.group(1), matcher.group(2));
            }
        }
    }

B:

    /**
     * parse all tag content to map, eg:<tag>value</tag>
     * @author jervalj
     * 
     * @param textString
     */
    private void parse2Map(String textString) {
        if (null != textString) {
            // matcher '<tag>value</tag>' string
            Pattern pattern = Pattern.compile("<(.*)>(.*)</\\1>", Pattern.DOTALL);
            Matcher matcher = pattern.matcher(textString);
            while (matcher.find()) {
                // group(1) is the 'tag',group(2) is 'value'
                mailMap.put(matcher.group(1), matcher.group(2));
            }
        }
    }

猜你喜欢

转载自jerval.iteye.com/blog/2289378