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:com.hichinaschool.flashcards.libanki.Utils.java

/**
 * Strip HTML but keep media filenames/*from   ww w. ja va 2  s.  c  o  m*/
 */
public static String stripHTMLMedia(String s) {
    Matcher imgMatcher = imgPattern.matcher(s);
    return stripHTML(imgMatcher.replaceAll(" $1 "));
}

From source file:org.apache.roller.weblogger.business.plugins.entry.SmileysPlugin.java

/**
 * Find occurences of ascii emoticons and turn them into HTML image pointers.
 *//*from  w w w.ja  v a2 s. c  o m*/
public String render(WeblogEntry entry, String text) {
    Matcher matcher = null;
    for (int i = 0; i < smileyPatterns.length; i++) {
        matcher = smileyPatterns[i].matcher(text);
        text = matcher.replaceAll(imageTags[i]);
    }
    return text;
}

From source file:com.khs.sherpa.processor.RestfulRequestProcessor.java

public String getParmeter(HttpServletRequest request, String value) {
    String currentUrl = UrlUtil.getPath(request);
    Pattern pattern = Pattern.compile("(\\{\\w+\\})");
    Matcher matcher = pattern.matcher(path);
    int cnt = 0;//from  www. j  ava  2 s  .c o m
    while (matcher.find()) {
        if (matcher.group().equals("{" + value + "}")) {
            String matcherText = matcher.replaceAll("([^/]*)");
            pattern = Pattern.compile(matcherText);
            matcher = pattern.matcher(currentUrl);
            matcher.find();
            return matcher.group(cnt + 1);
        }
        cnt++;
    }
    return null;
}

From source file:org.obiba.onyx.print.impl.DefaultPdfTemplateEngine.java

private String stripOnyxPrefix(String variablePath) {
    Assert.notNull(variablePath, "variablePath must not be null.");
    Matcher matcher = onyxPattern.matcher(variablePath);
    return matcher.replaceAll("");
}

From source file:no.dusken.momus.service.drive.GoogleDocsTextConverter.java

/**
 * Bold and italics are not marked with tags in GDocs, instead it is applied with CSS.
 * For instance:/*from www  . jav  a2s  .  c om*/
 * .c1{font-weight:bold}
 * lalala <span class="c1">bold</span>
 *
 * The classnames change each time, so need to dynamicall find it and change the span to <i> or <b>
 */
private String findItalicsAndBold(String body, String css) {
    Matcher italicsMatcher = italicStyleName.matcher(css);
    Matcher boldMatcher = boldStyleName.matcher(css);

    if (italicsMatcher.find()) {
        String italicSelectorName = italicsMatcher.group(1);

        Pattern italicClasses = Pattern.compile("<span class=\"" + italicSelectorName + "\">(.*?)</span>");
        Matcher spanMatcherItalics = italicClasses.matcher(body);

        body = spanMatcherItalics.replaceAll("<i>$1</i>"); // $1 means what is matched inside the parentheses in the pattern
    }

    if (boldMatcher.find()) {
        String boldSelectorName = boldMatcher.group(1);

        Pattern boldClasses = Pattern.compile("<span class=\"" + boldSelectorName + "\">(.*?)</span>");
        Matcher spanMatcherBold = boldClasses.matcher(body);

        body = spanMatcherBold.replaceAll("<b>$1</b>");
    }

    return body;
}

From source file:com.zia.freshdocs.cmis.CMISParser06.java

@Override
public NodeRef[] parseChildren(InputStream is) {
    NodeRef[] refs = new NodeRef[0];
    DocumentBuilder docBuilder = null;

    try {/*w w  w. j  av a2 s  . c  o  m*/
        Pattern pattern = Pattern.compile("&(?![a-zA-Z0-9]+;)");
        Matcher matcher = pattern.matcher(IOUtils.toString(is));
        String sanitized = matcher.replaceAll("&amp;");

        docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = docBuilder.parse(new ByteArrayInputStream(sanitized.getBytes()));

        // Iterate over all the entry nodes and build NodeRefs
        NodeList nodes = doc.getElementsByTagName("entry");
        NodeList children = null;
        Element node = null;
        NodeRef nodeRef = null;
        int n = nodes.getLength();

        for (int i = 0; i < n; i++) {
            if (refs.length == 0) {
                refs = new NodeRef[n];
            }

            node = (Element) nodes.item(i);
            children = node.getElementsByTagName("content");

            if (children.getLength() > 0) {
                Element contentNode = (Element) children.item(0);
                String content = null;
                nodeRef = new NodeRef();

                if (contentNode.hasAttribute("type")) {
                    nodeRef.setContentType(contentNode.getAttribute("type"));
                    content = contentNode.getAttribute("src");
                } else {
                    content = contentNode.getFirstChild().getNodeValue();
                }

                nodeRef.setContent(content);

                children = node.getElementsByTagName("title");
                if (children.getLength() > 0) {
                    nodeRef.setName(children.item(0).getFirstChild().getNodeValue());
                }

                children = node.getElementsByTagName("updated");
                if (children.getLength() > 0) {
                    nodeRef.setLastModificationDate(children.item(0).getFirstChild().getNodeValue());
                }

                children = node.getElementsByTagName("cmis:propertyString");
                int nChildren = children.getLength();

                if (nChildren > 0) {
                    for (int j = 0; j < nChildren; j++) {
                        Element child = (Element) children.item(j);

                        if (child.getAttribute("cmis:name").equals("BaseType")) {
                            NodeList valueNode = child.getElementsByTagName("cmis:value");
                            String baseType = valueNode.item(0).getFirstChild().getNodeValue();
                            nodeRef.setFolder(baseType.equals("folder"));
                        } else if (child.getAttribute("cmis:name").equals("LastModifiedBy")) {
                            NodeList valueNode = child.getElementsByTagName("cmis:value");
                            nodeRef.setLastModifiedBy(valueNode.item(0).getFirstChild().getNodeValue());
                        } else if (child.getAttribute("cmis:name").equals("VersionLabel")) {
                            NodeList valueNode = child.getElementsByTagName("cmis:value");
                            if (valueNode.getLength() > 0) {
                                nodeRef.setVersion(valueNode.item(0).getFirstChild().getNodeValue());
                            }
                        }
                    }
                }

                children = node.getElementsByTagName("cmis:propertyInteger");
                nChildren = children.getLength();

                if (nChildren > 0) {
                    for (int j = 0; j < nChildren; j++) {
                        Element child = (Element) children.item(j);

                        if (child.getAttribute("cmis:name").equals("ContentStreamLength")) {
                            NodeList valueNode = child.getElementsByTagName("cmis:value");
                            nodeRef.setContentLength(
                                    Long.valueOf(valueNode.item(0).getFirstChild().getNodeValue()));
                            break;
                        }
                    }
                }

                refs[i] = nodeRef;
            }
        }
    } catch (Exception e) {
        Log.e(CMIS.class.getSimpleName(), "Error getting children", e);
    }

    return refs;
}

From source file:httpget.HttpGet.java

private String redirectFromResponse() throws HttpGetException {
    String inputLine, responseStr = null;
    this.getBufferedReader();

    try {//from  w  w w.ja va2 s.com
        while ((inputLine = this.bufferedReader.readLine()) != null) {
            if (inputLine.matches(".*<a.*href=\"[^>]+\".*>.*")) {
                Pattern p1 = Pattern.compile(".*<a.*href=\"");
                Pattern p2 = Pattern.compile("\".*>.*");
                Matcher m1 = p1.matcher(inputLine);
                responseStr = m1.replaceAll("");
                Matcher m2 = p2.matcher(responseStr);
                responseStr = m2.replaceAll("");
                break;
            }
        }
    } catch (IOException e) {
        throw (new HttpGetException(
                "Redirect (code " + this.responseCode + ") aangegeven door pagina maar pagina niet leesbaar",
                this.url.toString(), e, redirected_from));
    }

    if (responseStr == null)
        throw (new HttpGetException(
                "Redirect (code " + this.responseCode
                        + ") aangegeven door pagina maar geen valide redirect-locatie",
                this.url.toString(), new Exception(), redirected_from));

    try {
        this.bufferedReader.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return responseStr;
}

From source file:cdscreator.ParseCLI.java

/**
 * Rutger Ozinga. ParseCLI Parses the commandline input and is able to
 * return the wanted option and value./*from   w w  w  .j  av  a  2s.  c  o  m*/
 *
 * @param args are commandline arguments.
 * @throws org.apache.commons.cli.ParseException an exception
 */
public ParseCLI(final String[] args) throws org.apache.commons.cli.ParseException {
    HelpFormatter helpForm = new HelpFormatter();
    Options cliOpt = new Options();
    cliOpt.addOption("h", "help", false, "Displays help");
    cliOpt.addOption("p", true, "Expects a path to a protein fasta file.");
    cliOpt.addOption("t", true, "Expects a path to a transcript fasta file.");
    cliOpt.addOption("nt", true, "Expects a path to place the new tab separated transcript file at.");
    cliOpt.addOption("o", true, "Expects a path to place the new tab separated protein file at");
    if (args.length == 0) {
        helpForm.printHelp("Please enter all the " + "options below. ", cliOpt);
        System.exit(0);
    } else {
        BasicParser parser = new BasicParser();
        CommandLine cliParser = parser.parse(cliOpt, args);
        if (cliParser.getOptions().length < 4) {
            System.out.println(
                    "Error : " + "Please enter all options in" + " order for this program to work" + "!\n");
            helpForm.printHelp("Please enter all of the  " + "option ", cliOpt);
            System.exit(0);
        } else {
            if (cliParser.hasOption("h") && cliParser.hasOption("help")) {
                helpForm.printHelp("Command Line Help:\n", cliOpt);
                System.exit(0);
            } else {
                String snpFileString = cliParser.getOptionValue("p");
                Path snpPath = Paths.get(snpFileString);
                if (Files.exists(snpPath)) {
                    setProtPath(snpPath);
                } else {
                    System.out.println("The entered Path does" + " not exits");
                    helpForm.printHelp("Please enter -p followed by a valid" + " path ", cliOpt);
                    System.exit(0);
                }
                String transcriptFileString = cliParser.getOptionValue("t");
                Path transcriptPath = Paths.get(transcriptFileString);
                if (Files.exists(transcriptPath)) {
                    setTransPath(transcriptPath);
                } else {
                    System.out.println("The entered Path does" + " not exits");
                    helpForm.printHelp("Please enter -t followed by a valid" + " path ", cliOpt);
                    System.exit(0);
                }
                String newFileString = cliParser.getOptionValue("o");
                Matcher match = re.matcher(newFileString);
                String editedFileString = match.replaceAll("");
                Path newPath = Paths.get(editedFileString);
                if (Files.exists(newPath)) {
                    setNewFilePath(newFileString);
                } else {
                    System.out.println("The entered Path does" + " not exits");
                    helpForm.printHelp("Please enter -o followed by a valid" + " path ", cliOpt);
                    System.exit(0);
                }
                String newTranscriptFileString = cliParser.getOptionValue("nt");
                Matcher match2 = re.matcher(newTranscriptFileString);
                String editedTranscriptFileString = match2.replaceAll("");
                Path newTranscriptPath = Paths.get(editedTranscriptFileString);
                if (Files.exists(newTranscriptPath)) {
                    setNewTranscriptFilePath(newTranscriptFileString);
                } else {
                    System.out.println("The entered Path does" + " not exits");
                    helpForm.printHelp("Please enter -nt followed by a valid" + " path ", cliOpt);
                    System.exit(0);
                }
            }
        }
    }
}

From source file:org.madsonic.ajax.ChatService.java

private String ReplaceUrl(String message) {

    Pattern patt = Pattern
            .compile("(?<!\\S)(((f|ht){1}tp[s]?:\\/\\/|(?<!\\S)www\\.)[-a-zA-Z0-9@:%_\\+.~#?&\\/\\/=]+)");
    Matcher matcher = patt.matcher(message);
    if (matcher.find()) {
        if (matcher.group(1).startsWith("http://")) {
            return matcher.replaceAll("<a href=\"$1\" target=_blank>$1</a>");
        } else {/*  w ww .jav  a2  s .  c  o  m*/
            return matcher.replaceAll("<a href=\"http://$1\" target=_blank>$1</a>");
        }
    } else {
        return message;
    }
}

From source file:com.adaptris.core.services.metadata.ReplaceMetadataValue.java

@Override
public String reformat(String src, AdaptrisMessage msg) throws Exception {
    Matcher searchMatcher = searchPattern.matcher(src);
    String replacement = buildReplacementValue(searchMatcher, msg.resolve(replacementValue()));
    return replaceAll() ? searchMatcher.replaceAll(replacement) : searchMatcher.replaceFirst(replacement);
}