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:edu.hm.muse.controller.ImageLibraryController.java

@RequestMapping(value = "/picturegallery.secu", method = RequestMethod.GET)
public ModelAndView showImageLibrary(@RequestParam(value = "pic", required = false) String showPic) {

    String[] pics = new String[] { "pic1", "pic2", "pic3" };

    ModelAndView mv = new ModelAndView("picturegallery");

    String savepic = showPic + "hallowelt";
    savepic = savepic.replaceFirst("hallowelt", " ");
    savepic = savepic.trim();//from ww  w  . j av a 2  s.c  o m

    mv.addObject("pictures", pics);
    if (null != showPic && !showPic.isEmpty()) {
        mv.addObject("picture", savepic);
    }
    return mv;

}

From source file:com.cinchapi.concourse.shell.SyntaxTools.java

/**
 * Check to see if {@code line} is a command that uses short syntax. Short
 * syntax allows the user to call an API method without starting the command
 * with {@code concourse.}. This method compares the line to the list of
 * {@code options} to see if it should be "expanded" from short syntax.
 * Otherwise, the original line is returned.
 * /*from  w  w  w  .  j  av  a2s.  c om*/
 * @param line
 * @param options
 * @return the expanded line, if it is using short syntax, otherwise the
 *         original line
 */
public static String handleShortSyntax(String line, List<String> options) {
    final String prepend = "concourse.";
    if (line.equalsIgnoreCase("time") || line.equalsIgnoreCase("date")) {
        return line + " \"now\"";
    } else if (!line.contains("(")) {
        // If there are no parens in the line, then we assume that this is a
        // single(e.g non-nested) function invocation.
        if (line.startsWith(prepend)) {
            boolean hasArgs = line.split("\\s+").length > 1;
            if (!hasArgs) {
                line += "()";
            }
            return line;
        } else {
            String[] query = line.split("\\s+");
            String cmd = query[0];
            if (cmd.contains("_")) { // CON-457,GH-182
                String replacement = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, cmd);
                line = line.replaceFirst(cmd, replacement);
            }
            String expanded = prepend + line.trim();
            Pattern pattern = Pattern.compile(expanded.split("\\s|\\(")[0]);
            for (String option : options) {
                if (pattern.matcher(option).matches()) {
                    boolean hasArgs = expanded.split("\\s+").length > 1;
                    if (!hasArgs) {
                        expanded += "()";
                    }
                    return expanded;
                }
            }
        }
    } else {
        Set<String> shortInvokedMethods = parseShortInvokedMethods(line);
        for (String method : shortInvokedMethods) {
            if (options.contains(prepend + method)) {
                line = line.replaceAll("(?<!\\_)" + method + "\\(", prepend + method + "\\(");
            }
        }
    }
    return line;
}

From source file:de.weltraumschaf.groundzero.filter.PathPrefixRemover.java

@Override
public String process(final String input) {
    if (input.startsWith(prefix)) {
        return input.replaceFirst(prefix, REPLACEMENT);
    }/*from  w  ww. j  a  va  2  s  . c  o m*/

    return input;
}

From source file:com.qmetry.qaf.automation.util.StringUtil.java

public static boolean seleniumEquals(String expectedPattern, String actual) {

    if ((expectedPattern == null) || (actual == null)) {
        return expectedPattern == actual;
    }/*from   ww  w .j av  a2s .  c  o m*/
    if (actual.startsWith("regexp:") || actual.startsWith("regex:") || actual.startsWith("regexpi:")
            || actual.startsWith("regexi:") || actual.startsWith("start:") || actual.startsWith("end:")
            || actual.startsWith("in:")) {
        // swap 'em
        String tmp = actual;
        actual = expectedPattern;
        expectedPattern = tmp;
    }
    if (expectedPattern.startsWith("start:")) {
        return actual.startsWith(expectedPattern.replaceFirst("start:", ""));
    }
    if (expectedPattern.startsWith("end:")) {
        return actual.endsWith(expectedPattern.replaceFirst("end:", ""));
    }
    if (expectedPattern.startsWith("in:")) {
        return actual.contains(expectedPattern.replaceFirst("in:", ""));
    }
    Boolean b;
    b = handleRegex("regexp:", expectedPattern, actual, 0);
    if (b != null) {
        return b.booleanValue();
    }
    b = handleRegex("regex:", expectedPattern, actual, 0);
    if (b != null) {
        return b.booleanValue();
    }
    b = handleRegex("regexpi:", expectedPattern, actual, Pattern.CASE_INSENSITIVE);
    if (b != null) {
        return b.booleanValue();
    }
    b = handleRegex("regexi:", expectedPattern, actual, Pattern.CASE_INSENSITIVE);
    if (b != null) {
        return b.booleanValue();
    }

    if (expectedPattern.startsWith("exact:")) {
        String expectedExact = expectedPattern.replaceFirst("exact:", "");
        if (!expectedExact.equals(actual)) {
            System.out.println("expected " + actual + " to match " + expectedPattern);
            return false;
        }
        return true;
    }

    String expectedGlob = expectedPattern.replaceFirst("glob:", "");
    expectedGlob = expectedGlob.replaceAll("([\\]\\[\\\\{\\}$\\(\\)\\|\\^\\+.])", "\\\\$1");

    expectedGlob = expectedGlob.replaceAll("\\*", ".*");
    expectedGlob = expectedGlob.replaceAll("\\?", ".");
    if (!Pattern.compile(expectedGlob, Pattern.DOTALL).matcher(actual).matches()) {
        System.out.println("expected \"" + actual + "\" to match glob \"" + expectedPattern
                + "\" (had transformed the glob into regexp \"" + expectedGlob + "\"");
        return false;
    }
    return true;
}

From source file:com.tasktop.c2c.server.hudson.configuration.service.HudsonJobConfigurator.java

/**
 * @param configXml//from w ww.  ja  v  a  2 s  .c o m
 * @param newId
 * @return
 */
private String addIdProperty(String configXml, String newId) {
    return configXml.replaceFirst("<project>", "<project>\n  <id>" + newId + "</id>\n");
}

From source file:org.sakaiproject.lessonbuildertool.util.ResourceLoaderMessageSource.java

public void setBasename(String baseName) {
    if (baseName.startsWith("classpath:")) {
        baseName = baseName.replaceFirst("classpath:", "");
        baseName = baseName.replaceAll("/", ".");
    }/*from w  ww.j  a v  a2 s . co m*/
    this.resourceLoader = new ResourceLoader(baseName);
}

From source file:com.main.mailer.UploadPdf.java

public void savePdf(String imgv, String fileName, String filePath, String fileType) {
    try {/*from  w  w w .jav a  2  s .c om*/
        imgv = imgv.replaceFirst("^data:application/[^;]*;base64,?", "");
        // Converting a Base64 String into Image byte array
        byte[] imageByteArray = decodeImage(imgv);

        // Write a image byte array into file system
        FileOutputStream imageOutFile = new FileOutputStream(filePath + fileName + fileType);
        imageOutFile.write(imageByteArray);

        imageOutFile.close();

        System.out.println("Pdf Successfully Uploaded!");

    } catch (FileNotFoundException e) {
        System.out.println("Image not found" + e);
    } catch (IOException ioe) {
        System.out.println("Exception while reading the Image " + ioe);
    }

}

From source file:org.envirocar.server.rest.resources.JSONSchemaResource.java

@GET
@Produces(MediaType.APPLICATION_JSON)/* w w w  .j  a  va 2s  .c o  m*/
public JsonNode get() throws TrackNotFoundException {
    ObjectNode root = nodeFactory.objectNode();
    ArrayNode schemas = root.putArray(JSONConstants.SCHEMA);
    UriBuilder builder = getUriInfo().getRequestUriBuilder().path("{schema}");
    for (String path : this.schemaPaths) {
        schemas.add(builder.build(path.replaceFirst("/schema/", "")).toString());
    }
    return root;
}

From source file:org.sakaiproject.adminsiteperms.tool.ResourceLoaderMessageSource.java

public void setBasename(String baseName) {
    if (baseName.startsWith("classpath:")) {
        baseName = baseName.replaceFirst("classpath:", "");
        baseName = baseName.replaceAll("/", ".");
    }//from   w w w.  ja  va2s .c  o  m
    this.resourceLoader = new ResourceLoader(baseName, ResourceLoaderMessageSource.class.getClassLoader());
    log.info("Sakai-SitePerms i18n MSG basename init: " + baseName);
}

From source file:com.stratio.cassandra.index.schema.ColumnMapperBlob.java

/** {@inheritDoc} */
@Override/* w  w  w . j  av  a  2 s.com*/
public String indexValue(String name, Object value) {
    if (value == null) {
        return null;
    } else if (value instanceof ByteBuffer) {
        ByteBuffer bb = (ByteBuffer) value;
        return ByteBufferUtils.toHex(bb);
    } else if (value instanceof byte[]) {
        byte[] bytes = (byte[]) value;
        return ByteBufferUtils.toHex(bytes);
    } else if (value instanceof String) {
        String string = (String) value;
        string = string.replaceFirst("0x", "");
        byte[] bytes = Hex.hexToBytes(string);
        return Hex.bytesToHex(bytes);
    } else {
        throw new IllegalArgumentException(String.format("Value '%s' cannot be cast to byte array", value));
    }
}