Example usage for java.lang String replaceFirst

List of usage examples for java.lang String replaceFirst

Introduction

In this page you can find the example usage for java.lang String replaceFirst.

Prototype

public String replaceFirst(String regex, String replacement) 

Source Link

Document

Replaces the first substring of this string that matches the given regular expression with the given replacement.

Usage

From source file:com.iana.boesc.utility.BOESCUtil.java

public static String trimLeadingZeros(String inputString) {
    return inputString.replaceFirst(LEADING_ZERO_REGEX, "");
}

From source file:com.asual.summer.core.util.RequestUtils.java

public static String contextRelative(String uri, boolean contextRelative) {
    if (uri != null && uri.startsWith("/")) {
        String contextPath = getRequest().getContextPath();
        uri = uri.replaceFirst("^" + contextPath + "/?", "/");
        if (contextRelative) {
            uri = contextPath.concat(uri);
        }/*from   w w  w  . ja  va 2 s . co m*/
    }
    return uri;
}

From source file:Main.java

/**
 * Creates a new thread name with the given pattern
 * <p/>//from   w  w  w  . ja v  a  2s. co  m
 * @param pattern the pattern
 * @param name    the name
 * @return the thread name, which is unique
 */
public static String resolveThreadName(String pattern, String name) {
    if (pattern == null) {
        pattern = DEFAULT_PATTERN;
    }

    // we support #longName# and #name# as name placeholders
    String longName = name;
    String shortName = name;
    // must quote the names to have it work as literal replacement
    shortName = Matcher.quoteReplacement(shortName);
    longName = Matcher.quoteReplacement(longName);

    // replace tokens
    String answer = pattern.replaceFirst("#counter#", "" + nextThreadCounter());
    answer = answer.replaceFirst("#longName#", longName);
    answer = answer.replaceFirst("#name#", shortName);

    // are there any #word# combos left, if so they should be considered invalid tokens
    if (INVALID_PATTERN.matcher(answer).matches()) {
        throw new IllegalArgumentException("Pattern is invalid: " + pattern);
    }

    return answer;
}

From source file:Main.java

private static List<Node> getNode(Element element, String nodeWeiZhi) {
    List<Node> notes = new ArrayList<Node>();
    String[] nodeNames = nodeWeiZhi.split(">");
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        String nodeName = node.getNodeName();
        if (nodeName.equals(nodeNames[0])) {
            if (nodeNames.length == 1) {
                notes.add(node);//from w w w. j  a  va  2s. c  o  m
            }
            String nodeWeiZhiTemp = nodeWeiZhi.replaceFirst(nodeNames[0], "").replaceFirst(">", "");
            NodeList childrenTempList = node.getChildNodes();
            if (childrenTempList.getLength() > 0 && !nodeWeiZhiTemp.equals("")) {
                notes.addAll(getNode((Element) node, nodeWeiZhiTemp));
            }
        }
    }
    return notes;
}

From source file:controller.InputSanitizer.java

private static String checkAttachmentForFiles(String description, String uploadDir)
        throws NoSuchAlgorithmException, FileNotFoundException, IOException {
    String modifiedDescription = "";
    if (description.contains("href=\"/uploads/")) // suche andere Files (PDF, Code... etc)
    {//from  www  .j  ava  2s  . co  m
        // die Schleife ist notwenidig falls mehrere Dateien hinzugeuegt werden
        // andernfalls wuerde nur die erste zu MD5 
        String[] split = description.split("<a");
        String newSourceString = "href=\"" + uploadDir;
        System.out.println("newSourceString: " + newSourceString);

        if (uploadDir.equals("webapps/uploads/")) // if real server use this
        {
            newSourceString = "href=\"/uploads/";
        }

        for (String s : split) {
            if (s.contains("href=\"/uploads/")) {
                s = s.replaceFirst("href=\"/uploads/", newSourceString);

                int indexBegin = s.indexOf(newSourceString) + newSourceString.length();
                int indexEnd = s.indexOf("\"", indexBegin + 1);

                String oldFilename = s.substring(indexBegin, indexEnd);
                String file = uploadDir + s.substring(indexBegin, indexEnd);

                MessageDigest md = MessageDigest.getInstance("MD5");
                String digest = getDigest(new FileInputStream(file), md, 2048);

                File oldFile = new File(uploadDir + s.substring(indexBegin, indexEnd));
                File md5File = new File(uploadDir + digest);
                //  Funktioniert unter windows nicht
                if (!oldFile.renameTo(md5File)) {
                    System.out.println("Renaming went wrong");
                }

                s = s.replace(oldFilename, digest);
            }

            if (s.contains("href=\"http") || s.contains("href=\"www") || s.contains(newSourceString)) // http links abfangen
            {
                s = "<a" + s;
            }

            modifiedDescription = modifiedDescription + s;
        }
    } else {
        modifiedDescription = description;
    }

    System.out.println("descr: " + modifiedDescription);
    return modifiedDescription;
}

From source file:com.remobile.file.Filesystem.java

/**
 * Removes multiple repeated //s, and collapses processes ../s.
 *//*from ww w .  j  a v  a 2 s .c o m*/
protected static String normalizePath(String rawPath) {
    // If this is an absolute path, trim the leading "/" and replace it later
    boolean isAbsolutePath = rawPath.startsWith("/");
    if (isAbsolutePath) {
        rawPath = rawPath.replaceFirst("/+", "");
    }
    ArrayList<String> components = new ArrayList<String>(Arrays.asList(rawPath.split("/+")));
    for (int index = 0; index < components.size(); ++index) {
        if (components.get(index).equals("..")) {
            components.remove(index);
            if (index > 0) {
                components.remove(index - 1);
                --index;
            }
        }
    }
    StringBuilder normalizedPath = new StringBuilder();
    for (String component : components) {
        normalizedPath.append("/");
        normalizedPath.append(component);
    }
    if (isAbsolutePath) {
        return normalizedPath.toString();
    } else {
        return normalizedPath.toString().substring(1);
    }
}

From source file:Main.java

private static String getNodeText(Element element, String nodeWeiZhi) {
    String result = "";
    String[] nodeNames = nodeWeiZhi.split(">");
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        String nodeName = node.getNodeName();
        if (nodeName.equals(nodeNames[0])) {
            if (nodeNames.length == 1) {
                result += "," + node.getTextContent().trim();
            }/*w w  w .  ja  va2  s  . com*/
            String nodeWeiZhiTemp = nodeWeiZhi.replaceFirst(nodeNames[0], "").replaceFirst(">", "");
            NodeList childrenTempList = node.getChildNodes();
            if (childrenTempList.getLength() > 0 && !nodeWeiZhiTemp.equals("")) {
                result += getNodeText((Element) node, nodeWeiZhiTemp);
            }
        }
    }
    return result;
}

From source file:com.mymita.vaadlets.VaadletsBuilder.java

private static void setEmbeddedAttributes(final com.vaadin.ui.Component vaadinComponent,
        final com.mymita.vaadlets.core.Component vaadletsComponent) {
    if (vaadletsComponent instanceof com.mymita.vaadlets.ui.Embedded) {
        final String source = ((com.mymita.vaadlets.ui.Embedded) vaadletsComponent).getSource();
        if (source.startsWith("theme:")) {
            try {
                final URI uri = new URI(source);
                final String theme = uri.getHost();
                final String path = uri.getPath();
                final String resourceId = URLDecoder.decode(path.replaceFirst("/", ""),
                        Charsets.UTF_8.toString());
                ((com.vaadin.ui.Embedded) vaadinComponent).setSource(new ThemeResource(resourceId));
            } catch (final URISyntaxException e) {
            } catch (final UnsupportedEncodingException e) {
            }//  ww w.  j a v  a2 s. c  o  m
        }
    }
}

From source file:jongo.demo.Demo.java

private static void update(QueryRunner run, String stmt, Object... args) {
    if (args.length == 0) {
        //            System.out.println(stmt); // uncomment to print SQL
    } else {/*from  w w  w  .j av a2s  .  c  o  m*/
        String k = stmt;
        for (Object o : args) {
            String p = "";
            if (o instanceof java.lang.Number)
                p = String.valueOf(o);
            else
                p = "'" + String.valueOf(o) + "'";
            k = k.replaceFirst("\\?", p);
        }
        //            System.out.println(k); // uncomment to print SQL
    }
    try {
        run.update(stmt, args);
    } catch (SQLException ex) {
        l.error("Failed to update database", ex);
    }
}

From source file:Main.java

/**
 * Creates a new thread name with the given pattern
 * <p/>//from  w ww  .j  av a 2 s . c om
 * See {@link org.apache.camel.spi.ExecutorServiceManager#setThreadNamePattern(String)} for supported patterns.
 *
 * @param pattern the pattern
 * @param name    the name
 * @return the thread name, which is unique
 */
public static String resolveThreadName(String pattern, String name) {
    if (pattern == null) {
        pattern = DEFAULT_PATTERN;
    }

    // we support #longName# and #name# as name placeholders
    String longName = name;
    String shortName = name.contains("?") ? "" : name;
    // must quote the names to have it work as literal replacement
    shortName = Matcher.quoteReplacement(shortName);
    longName = Matcher.quoteReplacement(longName);

    // replace tokens
    String answer = pattern.replaceFirst("#counter#", "" + nextThreadCounter());
    answer = answer.replaceFirst("#longName#", longName);
    answer = answer.replaceFirst("#name#", shortName);

    // are there any #word# combos left, if so they should be considered invalid tokens
    if (INVALID_PATTERN.matcher(answer).matches()) {
        throw new IllegalArgumentException("Pattern is invalid: " + pattern);
    }

    return answer;
}