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

public static void main(String args[]) {
    Pattern p = Pattern.compile("(i|I)ce");
    String candidateString = "I love ice. Ice is my favorite. Ice Ice Ice.";

    Matcher matcher = p.matcher(candidateString);
    String tmp = matcher.replaceFirst("Java");
    System.out.println(tmp);// www  .  jav  a 2s. com
}

From source file:Main.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("(i|I)ce");
    String candidateString = "ice Ice Ice.";

    Matcher matcher = p.matcher(candidateString);
    String tmp = matcher.replaceFirst("Java");

    System.out.println(tmp);//from  w ww.j  av  a  2 s  .c  om
}

From source file:org.springframework.util.Pluralizer.java

public static void main(String[] args) {
    Pattern p = Pattern.compile("([^aeiouy]|qu)y$");
    Matcher m = p.matcher("ability");
    if (m.find()) {
        System.out.println(m.replaceFirst("\\1ies"));
    }//from  www  . j a va 2  s.c o  m
}

From source file:AIR.Common.Web.PatternUrlRewriter.java

public static void main(String[] args) {
    try {/*from   w  ww.j  a  v a 2 s  .  c  o  m*/
        Pattern p = Pattern.compile(
                "file:///D:/DataFiles/BB_Files/tds2_airws_org/TDSCore_2013-2014/Bank-([^/]*)/Items/Item-(?<bankid>[^/]*)-(?<itemid>[^/]*)/([^/]*)");

        Matcher m = p.matcher(
                "file:///D:/DataFiles/BB_Files/tds2_airws_org/TDSCore_2013-2014/Bank-179/Items/Item-195-6821/Item_22489_v5_rubric.xml");

        URL url = new URL(m.replaceFirst(
                "file:///C:/AIROSE Trainer Deployments/tmp/Datafiles/Item-${bankid}-${itemid}/Item_${itemid}_v5_rubric.xml"));
        System.err.println(url);
    } catch (Exception exp) {
        exp.printStackTrace();
    }
}

From source file:Main.java

/**
 * Creates a map of tag name to clean header XML by running a substitution
 * regex on each entry of tag to dirty header XML.
 *
 * @param dirtyXmlMap a map of tag name to dirty header XML
 * @return a map of tag name to clean header XML
 *///from  w ww.jav a  2  s  .co m
private static Map<String, String> createTagToCleanXmlMap(Map<String, String> dirtyXmlMap) {
    Map<String, String> cleanXmlMap = new HashMap<String, String>();
    for (Entry<String, String> sensitiveXml : dirtyXmlMap.entrySet()) {
        Pattern p = Pattern.compile(
                String.format(SENSITIVE_REGEX, sensitiveXml.getKey(), sensitiveXml.getKey()),
                Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
        Matcher m = p.matcher(sensitiveXml.getValue());
        if (m.matches()) {
            cleanXmlMap.put(sensitiveXml.getKey(), m.replaceFirst("$1******$2"));
        }
    }
    return cleanXmlMap;
}

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

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 {//from   w  ww . j ava  2s  .com
        return url;
    }
}

From source file:org.archive.modules.extractor.ExtractorImpliedURI.java

/**
 * Utility method for extracting 'implied' URI given a source uri, 
 * trigger pattern, and build pattern. /*from  ww w.  jav  a 2  s.  co m*/
 * 
 * @param uri source to check for implied URI
 * @param trigger regex pattern which if matched implies another URI
 * @param build replacement pattern to build the implied URI
 * @return implied URI, or null if none
 */
protected static String extractImplied(CharSequence uri, Pattern trigger, String build) {
    if (trigger == null) {
        return null;
    }
    Matcher m = trigger.matcher(uri);
    if (m.matches()) {
        String result = m.replaceFirst(build);
        return result;
    }
    return null;
}

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

/**
 * A utility method for masking the password in a file URI
 *
 * @param url   URL to be masked/*from ww  w  .  j a va2s  . co 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);
        return pwdMatcher.replaceFirst("\":***@\"");
    } else {
        return url;
    }
}

From source file:com.machinepublishers.jbrowserdriver.ResponseHandler.java

private static byte[] getBody(StreamConnection connection, byte[] inflatedContent, String url) {
    final Settings settings = SettingsManager.settings();
    try {/* w  w  w .j  a  v  a 2 s .c o m*/
        if (settings.quickRender() && ((StreamConnection) connection).isMedia()) {
            LogsServer.instance().trace("Media discarded: " + url);
            StatusMonitor.instance().addDiscarded(url);
            return new byte[0];
        } else if (!redirectCodes.contains(connection.getResponseCode())
                && (connection.getContentType() == null
                        || connection.getContentType().indexOf("text/html") > -1)
                && StatusMonitor.instance().isPrimaryDocument(false, url)) {
            String intercepted = null;
            String charset = Util.charset(connection);
            String content = new String(inflatedContent, charset);
            Matcher matcher = head.matcher(content);
            if (matcher.find()) {
                intercepted = matcher.replaceFirst(matcher.group(0) + settings.script());
            } else {
                matcher = html.matcher(content);
                if (matcher.find()) {
                    intercepted = matcher.replaceFirst(new StringBuilder().append(matcher.group(0))
                            .append("<head>").append(settings.script()).append("</head>").toString());
                } else {
                    matcher = body.matcher(content);
                    if (matcher.find()) {
                        intercepted = (new StringBuilder().append("<html><head>").append(settings.script())
                                .append("</head>").append(content).append("</html>").toString());
                    } else {
                        intercepted = content;
                    }
                }
            }
            return intercepted == null ? null : intercepted.getBytes(charset);
        }
    } catch (Throwable t) {
    }
    return null;
}

From source file:com.cyberway.issue.crawler.extractor.ExtractorImpliedURI.java

/**
 * Utility method for extracting 'implied' URI given a source uri, 
 * trigger pattern, and build pattern. //from   ww  w.j a v  a2 s .c om
 * 
 * @param uri source to check for implied URI
 * @param trigger regex pattern which if matched implies another URI
 * @param build replacement pattern to build the implied URI
 * @return implied URI, or null if none
 */
protected static String extractImplied(CharSequence uri, String trigger, String build) {
    if (trigger.length() == 0) {
        // short-circuit empty-string trigger
        return null;
    }
    Matcher m = TextUtils.getMatcher(trigger, uri);
    if (m.matches()) {
        String result = m.replaceFirst(build);
        TextUtils.recycleMatcher(m);
        return result;
    }
    return null;
}