Example usage for java.util.regex Matcher appendReplacement

List of usage examples for java.util.regex Matcher appendReplacement

Introduction

In this page you can find the example usage for java.util.regex Matcher appendReplacement.

Prototype

public Matcher appendReplacement(StringBuilder sb, String replacement) 

Source Link

Document

Implements a non-terminal append-and-replace step.

Usage

From source file:org.ops4j.ramler.generator.Names.java

/**
 * Builds a name for a constant, converting camel case to upper snake case. Example:
 * {@code httpMethod -> HTTP_METHOD}.
 *
 * @param source//from  w  ww.  j av a2 s.c om
 *            any name
 * @return upper case string with underscores as word separators
 */
public static String buildConstantName(String source) {
    Matcher m = CAMEL_CASE_PATTERN.matcher(source);

    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        m.appendReplacement(sb, "_" + m.group());
    }
    m.appendTail(sb);

    String friendlyName = sb.toString().toUpperCase();

    if (isDigits(left(friendlyName, 1))) {
        friendlyName = "_" + friendlyName;
    }

    return friendlyName;
}

From source file:org.ops4j.ramler.generator.Names.java

/**
 * Converts a camel case name to lower-case kebab case, using hyphens as separators.
 * E.g. {@code UserGroup -> user-group}.
 *
 * @param source camel case string//from w  w w. java  2  s  . c o  m
 * @return hyphen-separated lower-case string
 */
public static String buildLowerKebabCaseName(String source) {
    Matcher m = CAMEL_CASE_PATTERN.matcher(source);

    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        m.appendReplacement(sb, "-" + m.group());
    }
    m.appendTail(sb);

    String friendlyName = sb.toString().toLowerCase();

    if (isDigits(left(friendlyName, 1))) {
        friendlyName = "-" + friendlyName;
    }

    return friendlyName;
}

From source file:org.b3log.symphony.util.MP3Players.java

/**
 * Renders the specified content with MP3 player if need.
 *
 * @param content the specified content//  w w  w  .j a  v  a 2 s .  c o  m
 * @return rendered content
 */
public static final String render(final String content) {
    final StringBuffer contentBuilder = new StringBuffer();

    final Matcher m = PATTERN.matcher(content);
    while (m.find()) {
        String mp3URL = m.group();
        String mp3Name = StringUtils.substringBetween(mp3URL, "\">", ".mp3</a>");
        mp3URL = StringUtils.substringBetween(mp3URL, "href=\"", "\" rel=");

        m.appendReplacement(contentBuilder, "<div class=\"aplayer content-audio\" data-title=\"" + mp3Name
                + "\" data-url=\"" + mp3URL + "\" ></div>\n");
    }
    m.appendTail(contentBuilder);

    return contentBuilder.toString();
}

From source file:org.dllearner.confparser.PostProcessor.java

private static String replaceAllMap(String pre, Map<String, String> repMap, String post, String in) {
    List<String> keys = new ArrayList<>(repMap.keySet());
    Collections.sort(keys, (o1, o2) -> o1.length() - o2.length());
    CollectionUtils.transform(keys, input -> Pattern.quote(input));
    Matcher m = Pattern.compile(pre + "(" + Strings.join(keys, "|") + ")" + post).matcher(in);
    m.reset();//from   w ww .  ja va2  s  .c  o  m
    if (m.find()) {
        StringBuffer sb = new StringBuffer();
        do {
            m.appendReplacement(sb, repMap.get(m.group(1)));
        } while (m.find());
        return m.appendTail(sb).toString();
    }
    return in;
}

From source file:com.datumbox.common.utilities.PHPfunctions.java

public static String preg_replace(Pattern pattern, String replacement, String subject) {
    Matcher m = pattern.matcher(subject);
    StringBuffer sb = new StringBuffer(subject.length());
    while (m.find()) {
        m.appendReplacement(sb, replacement);
    }/*from  w ww.  ja  va2  s.c  o  m*/
    m.appendTail(sb);
    return sb.toString();
}

From source file:com.gs.obevo.db.impl.platforms.db2.Db2lookReveng.java

public static AbstractDdlReveng.LineParseOutput substituteTablespace(String input) {
    Pattern compile = Pattern.compile("(\\s+IN\\s+)\"(\\w+)\"(\\s*)", Pattern.DOTALL);

    StringBuffer sb = new StringBuffer(input.length());

    String addedToken = null;/*from   ww w  .j  ava  2s .  co  m*/
    String addedValue = null;
    Matcher matcher = compile.matcher(input);
    if (matcher.find()) {
        addedToken = matcher.group(2) + "_token";
        addedValue = matcher.group(2);
        matcher.appendReplacement(sb, matcher.group(1) + "\"\\${" + addedToken + "}\"" + matcher.group(3));
    }
    matcher.appendTail(sb);

    return new AbstractDdlReveng.LineParseOutput(sb.toString()).withToken(addedToken, addedValue);
}

From source file:Main.java

private static String makeEmailHerf(String content) {
    if (content.trim().length() == 0) {
        return content;
    }/*  w  w  w  .j a  v  a 2s .  c o  m*/
    StringBuffer emailStringBuffer = new StringBuffer();

    Matcher matcherEmail = patternEmail.matcher(content);
    while (matcherEmail.find()) {

        String email = matcherEmail.group();
        //            System.out.println("email:" + email);
        String emailToHref = "<a href=\"" + MailTo.MAILTO_SCHEME + email + "\">" + email + "</a>";
        matcherEmail.appendReplacement(emailStringBuffer, emailToHref);

    }
    matcherEmail.appendTail(emailStringBuffer);
    return emailStringBuffer.toString();
}

From source file:org.energyos.espi.common.test.FixtureFactory.java

public static String newXML(UUID uuid, String fileName) throws IOException {
    String s = newXML(fileName);/*w w w  .j av  a 2s.c o  m*/

    Pattern pattern = Pattern.compile("<id>urn:uuid:([A-Z0-9-]+)</id>");
    StringBuffer myStringBuffer = new StringBuffer();
    Matcher matcher = pattern.matcher(s);
    int i = 0;
    while (matcher.find()) {
        UUID replacement;
        if (i == 1) {
            replacement = uuid;
        } else {
            replacement = UUID.randomUUID();
        }
        matcher.appendReplacement(myStringBuffer,
                "<id>urn:uuid:" + replacement.toString().toUpperCase() + "</id>");
        i++;
    }
    matcher.appendTail(myStringBuffer);
    String subscription = myStringBuffer.toString().replaceAll("9B6C7066", uuid.toString());
    return subscription;
}

From source file:org.apache.fop.events.EventFormatter.java

private static int processIncludesInner(CharSequence template, StringBuffer sb, ResourceBundle bundle) {
    int replacements = 0;
    if (bundle != null) {
        Matcher m = INCLUDES_PATTERN.matcher(template);
        while (m.find()) {
            String include = m.group();
            include = include.substring(2, include.length() - 2);
            m.appendReplacement(sb, bundle.getString(include));
            replacements++;/*from  w  ww.  j a  v a  2  s. c  o m*/
        }
        m.appendTail(sb);
    }
    return replacements;
}

From source file:Main.java

private static String renderTelephone(String bodyHtml) {
    StringBuffer bodyStringBuffer = new StringBuffer();

    Matcher matcherTelContent = patternTagTitle.matcher(bodyHtml);
    while (matcherTelContent.find()) {

        String processContent = matcherTelContent.group(1);
        String htmlContent = matcherTelContent.group(2);
        if (htmlContent.equalsIgnoreCase("</script>") || htmlContent.equalsIgnoreCase("</a>")) {
            matcherTelContent.appendReplacement(bodyStringBuffer, processContent + htmlContent);
        } else {/*from w  w  w  .ja v  a 2s  .  c  o  m*/
            String telContentHasProcessed = makeTelNoHerf(processContent);
            matcherTelContent.appendReplacement(bodyStringBuffer, telContentHasProcessed + htmlContent);
        }
    }
    matcherTelContent.appendTail(bodyStringBuffer);
    return bodyStringBuffer.toString();
}