Example usage for java.util.regex Matcher replaceAll

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

Introduction

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

Prototype

public String replaceAll(Function<MatchResult, String> replacer) 

Source Link

Document

Replaces every subsequence of the input sequence that matches the pattern with the result of applying the given replacer function to the match result of this matcher corresponding to that subsequence.

Usage

From source file:org.exoplatform.forum.ForumTransformHTML.java

public static String cleanHtmlCode(String sms, List<String> bbcs) {
    if (sms == null || sms.trim().length() <= 0)
        return ForumUtils.EMPTY_STR;
    sms = StringUtils.replace(sms, "\n", " ");
    // clean bbcode
    List<String> bbcList = new ArrayList<String>();
    bbcList.addAll(bbcs);/* www .  j  av  a  2s  .  c  om*/
    for (String bbc : bbcs) {
        bbcList.add(bbc.toLowerCase());
    }
    int lastIndex = 0;
    int tagIndex = 0;
    String start, end;
    for (String bbc : bbcList) {
        start = "[" + bbc;
        end = "[/" + bbc + "]";
        lastIndex = 0;
        tagIndex = 0;
        while ((tagIndex = sms.indexOf(start, lastIndex)) != -1) {
            lastIndex = tagIndex + 1;
            try {
                int clsIndex = sms.indexOf(end, tagIndex);
                String content = sms.substring(tagIndex, clsIndex);
                String content_ = content.substring(content.indexOf("]") + 1);
                sms = StringUtils.replace(sms, content + end, content_);
            } catch (Exception e) {
                continue;
            }
        }
    }
    sms = StringUtils.replace(sms, "[U]", ForumUtils.EMPTY_STR);
    sms = StringUtils.replace(sms, "[/U]", ForumUtils.EMPTY_STR);
    sms = StringUtils.replace(sms, "[u]", ForumUtils.EMPTY_STR);
    sms = StringUtils.replace(sms, "[/u]", ForumUtils.EMPTY_STR);
    // Clean html code
    String scriptregex = "<(script|style)[^>]*>[^<]*</(script|style)>";
    Pattern p1 = Pattern.compile(scriptregex, Pattern.CASE_INSENSITIVE);
    Matcher m1 = p1.matcher(sms);
    sms = m1.replaceAll(ForumUtils.EMPTY_STR);
    String tagregex = "<[^>]*>";
    Pattern p2 = Pattern.compile(tagregex);
    Matcher m2 = p2.matcher(sms);
    sms = m2.replaceAll(ForumUtils.EMPTY_STR);
    String multiplenewlines = "(\\n{1,2})(\\s*\\n)+";
    sms = sms.replaceAll(multiplenewlines, "$1");
    return sms;
}

From source file:bin.spider.frame.uri.TextUtils.java

/**
 * Utility method using a precompiled pattern instead of using the
 * replaceAll method of the String class. This method will also be reusing
 * Matcher objects./*  w ww .  ja  v  a2s . c  o  m*/
 * 
 * @see java.util.regex.Pattern
 * @param pattern precompiled Pattern to match against
 * @param input the character sequence to check
 * @param replacement the String to substitute every match with
 * @return the String with all the matches substituted
 */
public static String replaceAll(String pattern, CharSequence input, String replacement) {
    input = new InterruptibleCharSequence(input);
    Matcher m = getMatcher(pattern, input);
    String res = m.replaceAll(replacement);
    recycleMatcher(m);
    return res;
}

From source file:de.ailis.jasdoc.tags.TagParser.java

/**
 * Removes all space and star characters from all lines which are not part
 * of the documentation itself./*from  www.  j  av a 2 s  . c  om*/
 * 
 * @param comment
 *            The JsDoc comment to trim.
 * @return The trimmed JsDoc comment.
 */
private static String trimLines(final String comment) {
    final Matcher matcher = TRIM_LINE_PATTERN.matcher(comment);
    if (matcher.matches())
        return matcher.replaceAll("$1");
    else
        return comment;
}

From source file:com.fujitsu.dc.test.jersey.box.dav.file.MoveServiceSourceHeaderValidateTest.java

/**
 * Etag???./* ww w.  j a v  a 2 s  .c  om*/
 * @param etag Etag
 * @return ?
 */
public static long getEtagVersion(String etag) {
    // version?
    Pattern pattern = Pattern.compile("^\"([0-9]+)-([0-9]+)\"$");
    Matcher m = pattern.matcher(etag);
    return Long.parseLong(m.replaceAll("$1"));
}

From source file:com.vmware.o11n.plugin.crypto.model.CryptoUtil.java

/**
 *
 * @param input//from www .j a  va 2  s . c  om
 * @param numChars
 * @return
 */
public static ArrayList<String> splitOnNumChars(String input, Integer numChars) {
    final Pattern pattern = Pattern.compile("\r\n|\n|\r");
    final Matcher matcher = pattern.matcher(input);
    String singleLine = matcher.replaceAll("");

    ArrayList<String> output = new ArrayList<>();

    while (singleLine.length() > 0) {
        String nextEntry = singleLine.substring(0, Math.min(numChars, singleLine.length()));
        output.add(nextEntry);
        singleLine = singleLine.substring(Math.min(numChars, singleLine.length()));
    }

    return output;
}

From source file:de.jgoestl.massdatagenerator.MassDataGenerator.java

private static StringBuilder generateOutput(int numberOfData, String inputString, DateFormat dateFormat) {
    StringBuilder output = new StringBuilder();
    for (int i = 1; i <= numberOfData; i++) {
        String part = inputString;

        Matcher uuidMatcher = uuidPattern.matcher(part);
        if (uuidMatcher.find()) {
            part = uuidMatcher.replaceAll(UUID.randomUUID().toString());
        }/*from w  w w. ja  v a2  s . c o  m*/

        Matcher seqMatcher = seqPattern.matcher(part);
        if (seqMatcher.find()) {
            part = seqMatcher.replaceAll(String.valueOf(i));
        }

        Matcher dateMatcher = datePattern.matcher(part);
        if (dateMatcher.find()) {
            Calendar now = Calendar.getInstance();
            part = dateMatcher.replaceAll(getFormattedDate(now, dateFormat));
        }

        output.append(part).append("\r\n");
    }
    return output;
}

From source file:ai.susi.mind.SusiPhrase.java

public static String normalizeExpression(String s) {
    s = s.toLowerCase().replaceAll("\\#", "  ");
    Matcher m;
    while (new TimeoutMatcher(m = wspace.matcher(s)).find())
        s = m.replaceAll(" ");
    while (new TimeoutMatcher(m = dspace.matcher(s)).find())
        s = m.replaceAll(" ");
    s = s.trim();//from  w  ww . j a va  2  s .  co  m
    if (s.startsWith("susi "))
        s = s.substring(5); // cut off susi address
    if (".?!".indexOf(s.charAt(s.length() - 1)) >= 0)
        s = s.substring(0, s.length() - 1).trim();
    // to be considered: https://en.wikipedia.org/wiki/Wikipedia:List_of_English_contractionst
    int p = -1;
    while ((p = s.toLowerCase().indexOf("it's ")) >= 0)
        s = s.substring(0, p + 2) + " is " + s.substring(p + 5);
    while ((p = s.toLowerCase().indexOf("what's ")) >= 0)
        s = s.substring(0, p + 4) + " is " + s.substring(p + 7);
    return s;
}

From source file:org.exoplatform.cms.common.TransformHTML.java

public static String cleanHtmlCode(String sms, List<String> bbcs) {
    if (isEmpty(sms))
        return EMPTY_STR;
    sms = StringUtils.replace(sms, "\n", SPACE);
    // clean bbcode
    if (bbcs != null && bbcs.size() > 0) {
        List<String> bbcList = new ArrayList<String>();
        bbcList.addAll(bbcs);//from w ww  .  ja  v  a 2  s . c  o m
        for (String bbc : bbcs) {
            bbcList.add(bbc.toLowerCase());
        }
        int lastIndex = 0;
        int tagIndex = 0;
        String start, end;
        for (String bbc : bbcList) {
            start = "[" + bbc;
            end = "[/" + bbc + "]";
            lastIndex = 0;
            tagIndex = 0;
            while ((tagIndex = sms.indexOf(start, lastIndex)) != -1) {
                lastIndex = tagIndex + 1;
                try {
                    int clsIndex = sms.indexOf(end, tagIndex);
                    String content = sms.substring(tagIndex, clsIndex);
                    String content_ = content.substring(content.indexOf("]") + 1);
                    sms = StringUtils.replace(sms, content + end, content_);
                } catch (Exception e) {
                    continue;
                }
            }
        }
        sms = StringUtils.replace(sms, "[U]", EMPTY_STR);
        sms = StringUtils.replace(sms, "[/U]", EMPTY_STR);
        sms = StringUtils.replace(sms, "[u]", EMPTY_STR);
        sms = StringUtils.replace(sms, "[/u]", EMPTY_STR);
    }
    // Clean html code
    String scriptregex = "<(script|style)[^>]*>[^<]*</(script|style)>";
    Pattern p1 = Pattern.compile(scriptregex, Pattern.CASE_INSENSITIVE);
    Matcher m1 = p1.matcher(sms);
    sms = m1.replaceAll(EMPTY_STR);
    String tagregex = "<[^>]*>";
    Pattern p2 = Pattern.compile(tagregex);
    Matcher m2 = p2.matcher(sms);
    sms = m2.replaceAll(EMPTY_STR);
    String multiplenewlines = "(\\n{1,2})(\\s*\\n)+";
    sms = sms.replaceAll(multiplenewlines, "$1");
    return sms;
}

From source file:org.apache.http.contrib.auth.AWSScheme.java

/**
 * Returns the canonicalized AMZ headers.
 *
 * @param headers/*  ww w. ja v a2  s  .  com*/
 *            The list of request headers.
 * @return The canonicalized AMZ headers.
 */
private static String getCanonicalizedAmzHeaders(final Header[] headers) {
    StringBuilder sb = new StringBuilder();
    Pattern spacePattern = Pattern.compile("\\s+");

    // Create a lexographically sorted list of headers that begin with x-amz
    SortedMap<String, String> amzHeaders = new TreeMap<String, String>();
    for (Header header : headers) {
        String name = header.getName().toLowerCase();

        if (name.startsWith("x-amz-")) {
            String value = "";

            if (amzHeaders.containsKey(name))
                value = amzHeaders.get(name) + "," + header.getValue();
            else
                value = header.getValue();

            // All newlines and multiple spaces must be replaced with a
            // single space character.
            Matcher m = spacePattern.matcher(value);
            value = m.replaceAll(" ");

            amzHeaders.put(name, value);
        }
    }

    // Concatenate all AMZ headers
    for (Entry<String, String> entry : amzHeaders.entrySet()) {
        sb.append(entry.getKey()).append(':').append(entry.getValue()).append("\n");
    }

    return sb.toString();
}

From source file:org.fao.geonet.MetadataResourceDatabaseMigration.java

public static boolean updateMetadataResourcesLink(@Nonnull Element xml, @Nullable String uuid,
        SettingManager settingManager) throws JDOMException {
    boolean changed = false;

    if (uuid == null) {
        final Element uuidElement = Xml.selectElement(xml, "gmd:fileIdentifier/gco:CharacterString",
                NAMESPACES);/*from   ww  w.  j  av a  2s  . c  o m*/
        if (uuidElement != null) {
            uuid = uuidElement.getText();
        }
    }

    if (StringUtils.isNotEmpty(uuid)) {
        @SuppressWarnings("unchecked")
        final List<Element> links = Lists
                .newArrayList((Iterable<? extends Element>) Xml.selectNodes(xml, XPATH_RESOURCES));

        for (Element element : links) {
            final String url = element.getText();
            Matcher regexMatcher = pattern.matcher(url);
            element.setText(regexMatcher
                    .replaceAll(settingManager.getNodeURL() + "api/records/" + uuid + "/attachments/$4"));
            changed = true;
        }

        // ATTACHMENTS
        // This fix the imports of metadata with attachments
        @SuppressWarnings("unchecked")
        final List<Element> linksAttachmentsUrl = Lists.newArrayList(
                (Iterable<? extends Element>) Xml.selectNodes(xml, XPATH_ATTACHMENTS_WITH_URL, NAMESPACES));

        for (Element element : linksAttachmentsUrl) {
            final String url = element.getText();
            if (url.indexOf("api/records/") > 0) {
                element.setText(url.replace(url.substring(0, url.indexOf("api/records/")),
                        settingManager.getNodeURL()));
                changed = true;
            }
        }

        // THUMBNAILS
        // This fix the imports from older versions of GN
        // where the thumbnails contains just the filename
        @SuppressWarnings("unchecked")
        final List<Element> linksThumbnailsNoUrl = Lists.newArrayList(
                (Iterable<? extends Element>) Xml.selectNodes(xml, XPATH_THUMBNAIL_WITH_NO_URL, NAMESPACES));

        for (Element element : linksThumbnailsNoUrl) {
            final String filename = element.getText();
            element.setText(String.format("%sapi/records/" + uuid + "/attachments/%s",
                    settingManager.getNodeURL(), filename));
            changed = true;
        }

        // This fix the imports from current versions of GN
        // where the thumbnails contains the full URL of the resource
        @SuppressWarnings("unchecked")
        final List<Element> linksThumbnailsWithUrl = Lists.newArrayList(
                (Iterable<? extends Element>) Xml.selectNodes(xml, XPATH_THUMBNAIL_WITH_URL, NAMESPACES));

        for (Element element : linksThumbnailsWithUrl) {
            final String url = element.getText();
            if (url.indexOf("api/records/") > 0) {
                element.setText(url.replace(url.substring(0, url.indexOf("api/records/")),
                        settingManager.getNodeURL()));
                changed = true;
            }
        }
    } else {
        throw new UnsupportedOperationException("Metadata is not supported. UUID is not defined.");
    }
    return changed;
}