Java Regex String Replace HTML replaceTagsUnlessAnchorTagFound(String wikiString, String searchPattern, String replacementPattern)

Here you can find the source of replaceTagsUnlessAnchorTagFound(String wikiString, String searchPattern, String replacementPattern)

Description

replace Tags Unless Anchor Tag Found

Declaration

private static String replaceTagsUnlessAnchorTagFound(String wikiString, String searchPattern,
            String replacementPattern) 

Method Source Code

//package com.java2s;

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

public class Main {
    private static Pattern aPattern = null;
    private static Matcher aMatcher = null;

    private static String replaceTagsUnlessAnchorTagFound(String wikiString, String searchPattern,
            String replacementPattern) {
        boolean codeTagsFound = stringContainsPattern(wikiString, searchPattern);
        boolean hrefTagsFound = wikiString.contains("[["); // [[url][desc]]
        if (codeTagsFound && hrefTagsFound) {
            return wikiString; // don't modify the string, we'll just mess it up
        } else if (codeTagsFound) {
            aPattern = Pattern.compile(searchPattern);
            aMatcher = aPattern.matcher(wikiString);
            return aMatcher.replaceAll(replacementPattern);
        } else {//from  www  .j  a  va  2  s. c  om
            return wikiString;
        }
    }

    private static boolean stringContainsPattern(String wikiString, String searchPattern) {
        Pattern p = Pattern.compile(searchPattern);
        Matcher m = p.matcher(wikiString);
        return m.find();
    }
}

Related

  1. replaceHtml(String html)
  2. replaceHtmlEntities(final String text)
  3. replaceHtmlEntities(String aText, boolean preserveFormatting)
  4. replaceTags(String payload, Map tags)
  5. replaceTags(String str, Map tags)