Java Regex String Replace HTML replaceTags(String str, Map tags)

Here you can find the source of replaceTags(String str, Map tags)

Description

replace Tags

License

Apache License

Declaration

public static String replaceTags(String str, Map<String, String> tags) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.*;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static final transient Pattern patternTag = Pattern.compile("<([a-zA-Z0-9_]*)>");

    public static String replaceTags(String str, Map<String, String> tags) {
        StringBuffer ret = new StringBuffer();
        Matcher matcher = patternTag.matcher(str);
        while (matcher.find()) {
            String tag = matcher.group(1);
            String repl = tags.get(tag);
            if (repl == null) {
                matcher.appendReplacement(ret, "<" + tag + ">");
            } else {
                matcher.appendReplacement(ret, repl);
            }//from  ww  w  .j  a  v a 2s  . co m
        }
        matcher.appendTail(ret);
        return ret.toString();
    }
}

Related

  1. replaceHtml(String html)
  2. replaceHtml(String html)
  3. replaceHtmlEntities(final String text)
  4. replaceHtmlEntities(String aText, boolean preserveFormatting)
  5. replaceTags(String payload, Map tags)
  6. replaceTagsUnlessAnchorTagFound(String wikiString, String searchPattern, String replacementPattern)