Example usage for java.util.regex Matcher replaceFirst

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

Introduction

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

Prototype

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

Source Link

Document

Replaces the first 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.synyx.hades.dao.query.QueryUtils.java

/**
 * Creates a count projected query from the given orginal query.
 * /*www.j  a  v a2 s.co  m*/
 * @param originalQuery must not be {@literal null} or empty
 * @return
 */
public static String createCountQueryFor(String originalQuery) {

    Assert.hasText(originalQuery);

    Matcher matcher = COUNT_MATCH.matcher(originalQuery);
    return matcher.replaceFirst(COUNT_REPLACEMENT);
}

From source file:org.wso2.carbon.transport.filesystem.connector.server.util.FileTransportUtils.java

/**
 * A utility method for masking the password in a file URI
 *
 * @param url   URL to be masked//from   w w w  . j  a  va2 s .  c o  m
 * @return The masked URL
 */
public static String maskURLPassword(String url) {
    Matcher urlMatcher = URL_PATTERN.matcher(url);
    if (urlMatcher.find()) {
        Matcher pwdMatcher = PASSWORD_PATTERN.matcher(url);
        String maskUrl = pwdMatcher.replaceFirst("\":***@\"");
        return maskUrl;
    } else {
        return url;
    }
}

From source file:com.ibm.rpe.web.service.docgen.impl.GenerateBaseTemplate.java

private static String replaceSchemaUrl(String str) {
    String replaceString = "\"schemas/schema/schema.xsd\"";

    final Pattern myPattern = Pattern.compile("\"schemas/url_.*.xsd\"");
    Matcher m = myPattern.matcher(str);
    if (m.find()) {
        str = m.replaceFirst(replaceString);
    }/*from  ww w  .ja  va2s .co m*/

    return str;
}

From source file:com.yenlo.synapse.transport.vfs.VFSUtils.java

/**
 * Mask the password of the connection url with ***
 * @param url the actual url/*  w  ww.  ja v  a2 s .co m*/
 * @return the masked url
 */
public static String maskURLPassword(String url) {
    final Matcher urlMatcher = URL_PATTERN.matcher(url);
    String maskUrl;
    if (urlMatcher.find()) {
        final Matcher pwdMatcher = PASSWORD_PATTERN.matcher(url);
        maskUrl = pwdMatcher.replaceFirst("\":***@\"");
        return maskUrl;
    }
    return url;
}

From source file:com.ibm.rpe.web.service.docgen.impl.GenerateBaseTemplate.java

private static String replaceElementId(String str) {
    String replaceString = "<element id=\"%ELEMENT_ID%\"";
    String tmpReplace = "%TEMP_ELEMENT_ID%";

    final Pattern myPattern = Pattern.compile("<element id=\"\\d+\"");
    Matcher m = myPattern.matcher(str);
    while (m.find()) {
        str = m.replaceFirst(tmpReplace);
        m = myPattern.matcher(str);/*from w  w  w.  j  a v a2 s  . c om*/
    }

    str = str.replaceAll(tmpReplace, replaceString);
    return str;
}

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

/**
 * Utility method using a precompiled pattern instead of using the
 * replaceFirst method of the String class. This method will also be reusing
 * Matcher objects.//  www. ja v a 2s.  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 the first match with
 * @return the String with the first match substituted
 */
public static String replaceFirst(String pattern, CharSequence input, String replacement) {
    input = new InterruptibleCharSequence(input);
    Matcher m = getMatcher(pattern, input);
    String res = m.replaceFirst(replacement);
    recycleMatcher(m);
    return res;
}

From source file:com.enonic.cms.core.portal.PrettyPathNameCreator.java

private static String ensureNiceBeginningAndEnding(String prettifiedName) {
    if (StringUtils.isBlank(prettifiedName)) {
        return "";
    }/*from   www.  ja  v  a 2  s .  c o  m*/

    Matcher m = STRIP_BEGINNING_PATTERN.matcher(prettifiedName);

    if (m.matches()) {
        prettifiedName = m.replaceFirst(m.group(2));
    }

    m = STRIP_ENDING_PATTERN.matcher(prettifiedName);

    if (m.matches()) {
        prettifiedName = m.replaceFirst(m.group(1));
    }

    return prettifiedName;
}

From source file:org.kuali.rice.ksb.messaging.serviceconnectors.ServiceConnectorFactory.java

public static String determineAlternateEndpoint(ServiceConfiguration serviceConfiguration) {
    String alternateEndpointUrl = null;
    List<AlternateEndpointLocation> alternateEndpointLocations = (List<AlternateEndpointLocation>) ConfigContext
            .getCurrentContextConfig().getObject(KSBConstants.Config.KSB_ALTERNATE_ENDPOINT_LOCATIONS);
    if (alternateEndpointLocations != null) {
        for (AlternateEndpointLocation alternateEndpointLocation : alternateEndpointLocations) {
            if (Pattern.matches(".*" + alternateEndpointLocation.getEndpointHostReplacementPattern() + ".*",
                    serviceConfiguration.getEndpointUrl().toExternalForm())) {
                Pattern myPattern = Pattern
                        .compile(alternateEndpointLocation.getEndpointHostReplacementPattern());
                Matcher myMatcher = myPattern.matcher(serviceConfiguration.getEndpointUrl().toExternalForm());
                String alternateEndpoint = myMatcher
                        .replaceFirst(alternateEndpointLocation.getEndpointHostReplacementValue());
                if (LOG.isInfoEnabled()) {
                    LOG.info("Found an alternate url host value ("
                            + alternateEndpointLocation.getEndpointHostReplacementValue() + ") for endpoint: "
                            + serviceConfiguration.getEndpointUrl() + " -> instead using: "
                            + alternateEndpoint);
                }/*from   www .jav a 2 s.co m*/
                alternateEndpointUrl = alternateEndpoint;
                break;
            }
        }
    }
    List<AlternateEndpoint> alternateEndpoints = (List<AlternateEndpoint>) ConfigContext
            .getCurrentContextConfig().getObject(KSBConstants.Config.KSB_ALTERNATE_ENDPOINTS);
    if (alternateEndpoints != null) {
        for (AlternateEndpoint alternateEndpoint : alternateEndpoints) {
            if (Pattern.matches(alternateEndpoint.getEndpointUrlPattern(),
                    serviceConfiguration.getEndpointUrl().toExternalForm())) {
                if (LOG.isInfoEnabled()) {
                    LOG.info("Found an alternate url for endpoint: " + serviceConfiguration.getEndpointUrl()
                            + " -> instead using: " + alternateEndpoint.getActualEndpoint());
                }
                alternateEndpointUrl = alternateEndpoint.getActualEndpoint();
                break;
            }
        }
    }
    return alternateEndpointUrl;
}

From source file:eu.eubrazilcc.lvl.core.util.UrlUtils.java

/**
 * Parses a URL from a String. This method supports file-system paths 
 * (e.g. /foo/bar).//from www  . j a v a 2 s . co m
 * @param str - String representation of an URL
 * @return an URL.
 * @throws IOException If an input/output error occurs.
 */
public static @Nullable URL parseURL(final String str) throws MalformedURLException {
    URL url = null;
    if (isNotBlank(str)) {
        try {
            url = new URL(str);
        } catch (MalformedURLException e) {
            url = null;
            if (!str.matches("^[a-zA-Z]+[/]*:[^\\\\]")) {
                // convert path to UNIX path
                String path = separatorsToUnix(str.trim());
                final Pattern pattern = Pattern.compile("^([a-zA-Z]:/)");
                final Matcher matcher = pattern.matcher(path);
                path = matcher.replaceFirst("/");
                // convert relative paths to absolute paths
                if (!path.startsWith("/")) {
                    path = path.startsWith("~") ? path.replaceFirst("~", System.getProperty("user.home"))
                            : concat(System.getProperty("user.dir"), path);
                }
                // normalize path
                path = normalize(path, true);
                if (isNotBlank(path)) {
                    url = new File(path).toURI().toURL();
                } else {
                    throw new MalformedURLException("Invalid path: " + path);
                }
            } else {
                throw e;
            }
        }
    }
    return url;
}

From source file:org.ambraproject.wombat.controller.WombatController.java

/**
 * Edit a "Content-Disposition" header value by changing a ".PNG_*" file extension to ".png". (The ".PNG_*" file
 * extensions are an ugly system quirk that we don't want to expose to the user.)
 *
 * @param contentDispositionValue a "Content-Disposition" header value
 * @return an edited value if it's bad; else the same value
 *//* w  w  w  .  j  a  va  2 s . co m*/
private static String sanitizeAssetFilename(String contentDispositionValue) {
    Matcher matcher = BAD_THUMBNAIL_EXTENSION.matcher(contentDispositionValue);
    if (matcher.find()) {
        return matcher.replaceFirst(".png");
    }
    return contentDispositionValue;
}