Example usage for org.apache.commons.lang3 StringUtils replace

List of usage examples for org.apache.commons.lang3 StringUtils replace

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils replace.

Prototype

public static String replace(final String text, final String searchString, final String replacement, int max) 

Source Link

Document

Replaces a String with another String inside a larger String, for the first max values of the search String.

A null reference passed to this method is a no-op.

 StringUtils.replace(null, *, *, *)         = null StringUtils.replace("", *, *, *)           = "" StringUtils.replace("any", null, *, *)     = "any" StringUtils.replace("any", *, null, *)     = "any" StringUtils.replace("any", "", *, *)       = "any" StringUtils.replace("any", *, *, 0)        = "any" StringUtils.replace("abaa", "a", null, -1) = "abaa" StringUtils.replace("abaa", "a", "", -1)   = "b" StringUtils.replace("abaa", "a", "z", 0)   = "abaa" StringUtils.replace("abaa", "a", "z", 1)   = "zbaa" StringUtils.replace("abaa", "a", "z", 2)   = "zbza" StringUtils.replace("abaa", "a", "z", -1)  = "zbzz" 

Usage

From source file:kenh.expl.functions.Replace.java

public String process(String text, String searchString, String replacement, int max) {
    return StringUtils.replace(text, searchString, replacement, max);
}

From source file:io.thekraken.grok.api.Grok.java

/**
 * Compile the {@code Grok} pattern to named regex pattern.
 * //from w w w. j a va 2 s  . c  o m
 * @param pattern : Grok pattern (ex: %{IP})
 * @param namedOnly : Whether to capture named expressions only or not (i.e. %{IP:ip} but not ${IP})
 * @throws GrokException runtime expt
  */
public void compile(String pattern, boolean namedOnly) throws GrokException {

    if (StringUtils.isBlank(pattern)) {
        throw new GrokException("{pattern} should not be empty or null");
    }

    namedRegex = pattern;
    originalGrokPattern = pattern;
    int index = 0;
    /** flag for infinite recurtion */
    int iterationLeft = 1000;
    Boolean continueIteration = true;

    Matcher cm = GrokUtils.CUSTOM_PATTERN.matcher(namedRegex);

    while (cm.find()) {
        Map<String, String> group = GrokUtils.namedGroups(cm, cm.group());
        String customPattern = group.get("grok") == null ? group.get("custompattern")
                : grokPatternDefinition.get(group.get("grokpattern"));
        String replacement = String.format("(?<name%d>%s)", index, customPattern);
        namedRegexCollection.put("name" + index, group.get("customname"));
        namedRegex = StringUtils.replace(namedRegex, group.get("custom"), replacement, 1);
        index++;
    }

    // Replace %{foo} with the regex (mostly groupname regex)
    // and then compile the regex
    while (continueIteration) {
        continueIteration = false;
        if (iterationLeft <= 0) {
            throw new GrokException("Deep recursion pattern compilation of " + originalGrokPattern);
        }
        iterationLeft--;

        Matcher m = GrokUtils.GROK_PATTERN.matcher(namedRegex);
        // Match %{Foo:bar} -> pattern name and subname
        // Match %{Foo=regex} -> add new regex definition 
        if (m.find()) {
            continueIteration = true;
            Map<String, String> group = GrokUtils.namedGroups(m, m.group());
            if (group.get("definition") != null) {
                try {
                    addPattern(group.get("pattern"), group.get("definition"));
                    group.put("name", group.get("name") + "=" + group.get("definition"));
                } catch (GrokException e) {
                    // Log the exeception
                }
            }
            int count = StringUtils.countMatches(namedRegex, "%{" + group.get("name") + "}");
            for (int i = 0; i < count; i++) {
                String replacement = String.format("(?<name%d>%s)", index,
                        grokPatternDefinition.get(group.get("pattern")));
                if (namedOnly && group.get("subname") == null) {
                    replacement = grokPatternDefinition.get(group.get("pattern"));
                }
                namedRegexCollection.put("name" + index,
                        (group.get("subname") != null ? group.get("subname") : group.get("name")));
                namedRegex = StringUtils.replace(namedRegex, "%{" + group.get("name") + "}", replacement, 1);
                // System.out.println(_expanded_pattern);
                index++;
            }
        }
    }

    if (namedRegex.isEmpty()) {
        throw new GrokException("Pattern not fount");
    }
    // Compile the regex
    compiledNamedRegex = Pattern.compile(namedRegex);
}

From source file:io.sugo.grok.api.Grok.java

/**
 * Compile the {@code Grok} pattern to named regex pattern.
 * //from  w w  w  .java 2s . c  o  m
 * @param pattern : Grok pattern (ex: %{IP})
 * @param namedOnly : Whether to capture named expressions only or not (i.e. %{IP:ip} but not ${IP})
 * @throws GrokException runtime expt
  */
public void compile(String pattern, boolean namedOnly) throws GrokException {

    if (StringUtils.isBlank(pattern)) {
        throw new GrokException("{pattern} should not be empty or null");
    }

    namedRegex = pattern;
    originalGrokPattern = pattern;
    int index = 0;
    /** flag for infinite recurtion */
    int iterationLeft = 1000;
    Boolean continueIteration = true;

    // Replace %{foo} with the regex (mostly groupname regex)
    // and then compile the regex
    while (continueIteration) {
        continueIteration = false;
        if (iterationLeft <= 0) {
            throw new GrokException("Deep recursion pattern compilation of " + originalGrokPattern);
        }
        iterationLeft--;

        Matcher m = GrokUtils.GROK_PATTERN.matcher(namedRegex);
        // Match %{Foo:bar} -> pattern name and subname
        // Match %{Foo=regex} -> add new regex definition 
        if (m.find()) {
            continueIteration = true;
            Map<String, String> group = GrokUtils.namedGroups(m, m.group());
            if (group.get("definition") != null) {
                try {
                    addPattern(group.get("pattern"), group.get("definition"));
                    group.put("name", group.get("name") + "=" + group.get("definition"));
                } catch (GrokException e) {
                    // Log the exeception
                }
            }
            int count = StringUtils.countMatches(namedRegex, "%{" + group.get("name") + "}");
            for (int i = 0; i < count; i++) {
                String replacement = String.format("(?<name%d>%s)", index,
                        grokPatternDefinition.get(group.get("pattern")));
                if (namedOnly && group.get("subname") == null) {
                    replacement = grokPatternDefinition.get(group.get("pattern"));
                }
                namedRegexCollection.put("name" + index,
                        (group.get("subname") != null ? group.get("subname") : group.get("name")));
                namedRegex = StringUtils.replace(namedRegex, "%{" + group.get("name") + "}", replacement, 1);
                // System.out.println(_expanded_pattern);
                index++;
            }
        }
    }

    if (namedRegex.isEmpty()) {
        throw new GrokException("Pattern not fount");
    }
    // Compile the regex
    compiledNamedRegex = Pattern.compile(namedRegex);
}