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:Main.java

/**
 * Simple regular-expression based method to strip the outermost tags from a fragment of XML.
 * Assumes that the fragment begins and ends with tags.
 * @param xmlFragment//  w w  w  .ja v  a  2 s  . co m
 * @return the xmlFragment string with the outermost tags removed.
 */

public static String stripOutermostTags(String xmlFragment) {

    if (xmlFragment == null) {
        return null;
    }

    Pattern tagContainingPattern = Pattern.compile("^<.*?>(.*)</.*?>$");

    if (tagContainingPattern == null) {
        return xmlFragment;
    }

    Matcher tagContainingMatcher = tagContainingPattern.matcher(xmlFragment);

    if (tagContainingMatcher == null) {
        return xmlFragment;
    }

    if (tagContainingMatcher.find()) {
        tagContainingMatcher.group();
        return tagContainingMatcher.replaceAll("$1");
    }
    return xmlFragment; // *shrug* no tags in the fragment, apparently.
}

From source file:com.datumbox.framework.core.utilities.text.parsers.HTMLParser.java

/**
 * Replaces the img tags with their alt text.
 * /*from   w  ww. j  a  v  a 2s . c  o  m*/
 * @param html
 * @return 
 */
public static String replaceImgWithAlt(String html) {
    Matcher m = IMG_ALT_TITLE_PATTERN.matcher(html);
    if (m.find()) {
        return m.replaceAll(" $1 ");
    }
    return html;
}

From source file:com.datumbox.framework.core.utilities.text.parsers.HTMLParser.java

private static String removeNonTextTags(String html) {
    html = removeComments(html);//w  ww .  j a v a2s. c o  m
    Matcher m = NON_TEXT_TAGS_PATTERN.matcher(html);
    if (m.find()) {
        html = m.replaceAll(" ");
    }

    return html;
}

From source file:com.indoqa.lang.util.StringUtils.java

/**
 * Replaces control characters defined in CONTROL_CHARACTERS_PATTERN with the replacement string.
 *
 * @param text The text whose control characters are to be replaced.
 * @param replacement The text the control characters should be replaced with
 * @return The String with the replaced control characters or empty String if text is null or empty.
 * @throws IllegalArgumentException if the replacement string is <code>null</code>
 *//*from w  w w.  java2 s .  c om*/
public static String replaceControlCharactersWith(String text, String replacement) {
    if (org.apache.commons.lang3.StringUtils.isBlank(text)) {
        return "";
    }

    if (replacement == null) {
        throw new IllegalArgumentException("The replacement string can't be null");
    }

    // we don't use String.replaceAll because this would compile the pattern every time
    Matcher matcher = CONTROL_CHARACTERS_PATTERN.matcher(text);
    return matcher.replaceAll(replacement);
}

From source file:org.mentawai.util.StringUtils.java

public static String removeAccents(String text) {
    String result = text;/*from   w ww  . j  a va  2  s.  c o m*/
    for (int i = 0; i < PATTERNS.length; i++) {
        Matcher matcher = PATTERNS[i].matcher(result);
        result = matcher.replaceAll(REPLACES[i]);
    }
    return result;
}

From source file:com.datumbox.framework.core.utilities.text.parsers.HTMLParser.java

/**
 * Removes all non-text tags (Javascript, css etc) from a string along with
 * all the attributes from the tags.//w  ww  .j  a v  a 2s.c  o m
 * 
 * @param html
 * @return 
 */
public static String removeNonTextTagsAndAttributes(String html) {
    html = removeNonTextTags(html);

    Matcher m = REMOVE_ATTRIBUTES_PATTERN.matcher(html);
    if (m.find()) {
        html = m.replaceAll("<$1$2>");
    }

    html = StringEscapeUtils.unescapeHtml4(html);

    return html;
}

From source file:net.sf.jabref.importer.fetcher.MedlineFetcher.java

private static String toSearchTerm(String in) {
    // This can probably be simplified using simple String.replace()...
    String result = in;//w  w  w  .  j a v  a  2 s. co m
    Matcher matcher;
    matcher = PART1_PATTERN.matcher(result);
    result = matcher.replaceAll("\\+AND\\+");
    matcher = PART2_PATTERN.matcher(result);
    result = matcher.replaceAll("\\+AND\\+");
    result = result.replace(" ", "+");
    return result;
}

From source file:fm.audiobox.tests.mocks.MockHttp.java

/**
 * Gets transport./* w  w w .  j a v  a 2 s . c  o  m*/
 *
 * @param status           the status
 * @param responseFilePath the response file path
 *
 * @return the transport
 */
public static HttpTransport getTransport(final int status, final String responseFilePath) {

    if (Configuration.Env.development != AudioBoxTests.env) {
        return new NetHttpTransport();
    }

    return new MockHttpTransport() {

        @Override
        public LowLevelHttpRequest buildRequest(final String method, final String url) throws IOException {

            return new MockLowLevelHttpRequest() {

                @Override
                public LowLevelHttpResponse execute() throws IOException {

                    String fileName = responseFilePath;

                    if (fileName == null) {
                        Pattern p = Pattern.compile("^(.*\\:(80|443|3000|5000))?(.*/api/v1)?/");
                        Matcher m = p.matcher(url);
                        fileName = m.replaceAll("");
                    }

                    Pattern queryString = Pattern.compile("\\?.*$");
                    Matcher qStringMatcher = queryString.matcher(fileName);
                    boolean hasQueryString = false;
                    if (qStringMatcher.find()) {
                        hasQueryString = true;
                        fileName = qStringMatcher.replaceAll("");
                    }

                    MockLowLevelHttpResponse result = new MockLowLevelHttpResponse();
                    result.setStatusCode(status);

                    if (HttpMethods.DELETE.equals(method)) {
                        result.setStatusCode(HttpStatus.SC_NO_CONTENT);
                    }

                    result.setContentType(Json.MEDIA_TYPE);

                    if (!hasQueryString && !fileName.endsWith(".json") && !fileName.contains("daemon")) {
                        fileName += ".json";
                    }

                    if (url.endsWith("sync.json")) {
                        Pattern p = Pattern.compile("000_(dropbox|mega|soundcloud)");
                        Matcher m = p.matcher(url);
                        if (m.find()) {
                            result.setStatusCode(HttpStatus.SC_FORBIDDEN);
                        } else {
                            result.setStatusCode(HttpStatus.SC_NO_CONTENT);
                        }
                        return result;
                    }

                    if (HttpMethods.PUT.equals(method)) {
                        if (url.endsWith("000_cloud.json")) {
                            result.setStatusCode(HttpStatus.SC_NOT_FOUND);
                        }
                    }

                    // "since" param on get playlist media files
                    if (url.contains("since=")) {
                        fileName = "playlists/000_cloud/media_files_since.json";
                    }

                    try {
                        result.setContent(IOUtils.toString(
                                this.getClass().getResourceAsStream("/responses/" + fileName), "UTF-8"));
                    } catch (NullPointerException e) {
                        l.warn("Requested resource not found: /responses/" + fileName);
                        l.warn("Try to perform the request on:  " + url);
                        result.setStatusCode(HttpStatus.SC_NOT_FOUND);
                    }

                    if (HttpStatus.SC_NOT_FOUND == result.getStatusCode()) {
                        result.setReasonPhrase("Not Found");
                    }

                    if (HttpStatus.SC_INTERNAL_SERVER_ERROR == result.getStatusCode()) {
                        result.setReasonPhrase("Internal Server Error");
                    }

                    if (HttpStatus.SC_SERVICE_UNAVAILABLE == result.getStatusCode()) {
                        result.setReasonPhrase("Service Unavailable");
                        result.addHeader("Retry-After", "10");
                    }

                    return result;
                }
            };
        }
    };
}

From source file:net.sf.groovyMonkey.actions.PasteScriptFromClipboardAction.java

public static String collapseEscapedNewlines(final String input) {
    final Pattern pattern = compile("\\\\(\n|(\r\n?))");
    final Matcher match = pattern.matcher(input);
    final String result = match.replaceAll("\n");
    return result;
}

From source file:com.bluexml.tools.miscellaneous.PrepareSIDEModulesMigration.java

protected static String replaceAll(String readFileToString, String depRegExp, String depReplacement) {
    String replaced;/*  w  w  w.  j a  va 2  s  .  co m*/
    Pattern pattern = Pattern.compile(depRegExp);
    Matcher matcher = pattern.matcher(readFileToString);
    matcher.find();
    replaced = matcher.replaceAll(depReplacement);
    return replaced;
}