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:org.j2free.util.HtmlFilter.java

/**
 *
 * @param text/*  ww  w  .  j a v  a2 s.com*/
 * @return
 */
public String filterForEmail(String text) {
    Pattern links = Pattern.compile(LINK_PATTERN, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);

    Matcher matcher = links.matcher(text);
    text = matcher.replaceAll("$2 [link: $1]");

    // First replace <tr> and <br> with \n
    text = text.replaceAll(EMAIL_LINE_BREAKS, "\n");

    text = strictFilter(text);

    text = text.replaceAll(NOBR_SPACE_PATTERN, " ") // make all &nbsp; into plain text spaces
            .replaceAll("\t", " ") // make all tabs spaces
            .replaceAll(" {2,}", " ") // make all multiple space sections a single space
            .replaceAll("^\\s", "") // remove any leaving space
            .replaceAll("\n{2,}", "\n\n"); // make sure there aren't too many line breaks in a row

    // Get the text as lines
    String[] lines = text.split("\n");
    String[] words;

    StringBuilder body = new StringBuilder();

    int lineLength = 0;
    String lastWord;
    for (String line : lines) {
        words = line.split("\\s");
        lineLength = 0;

        lastWord = "";
        for (String word : words) {
            if (lineLength > 0 && lineLength + word.length() >= PLAINTEXT_LINE_LENGTH
                    && !lastWord.equals("[link:")) {
                body.append("\n" + word);
                lineLength = word.length();
            } else {
                body.append((lineLength > 0 ? " " : "") + word);
                lineLength += word.length();
            }
            lastWord = word;
        }
        body.append("\n");
    }
    return body.toString().replaceAll("\n{3,}", "\n\n");
}

From source file:edu.internet2.middleware.psp.ldap.LdapSpmlTarget.java

/**
 * Escape all forward slashes "/" with "\/".
 * //from   w w  w  .j  ava2 s . c o m
 * @param dn the ldap dn
 * @return the resultant string with / replaced with \/
 */
public static String escapeForwardSlash(final String dn) {

    Matcher matcher = forwardSlashPattern.matcher(dn);

    if (matcher.find()) {
        return matcher.replaceAll("$1\\\\/");
    }

    return dn;
}

From source file:gr.abiss.calipso.wicket.components.formfields.HumanTimeDurationConverter.java

/**
 * @param replacementsText//  www  .j a  v a2s.c om
 * @return
 */
private String removeSuperfluousWhitespace(String s) {
    Matcher matcher = whitespace.matcher(s);
    if (matcher.find()) {
        s = matcher.replaceAll(" ");
    }
    return s;
}

From source file:net.wastl.webmail.server.SystemCheck.java

public SystemCheck(WebMailServer parent) throws WebMailException {
    log.info("Checking Java Virtual Machine ... ");
    log.info("Version: " + System.getProperty("java.version") + " ... ");

    /* Test if the Java version might cause trouble */
    if (System.getProperty("java.version").compareTo("1.5") >= 0) {
        log.info("JDK version ok.");
    } else {/*from w ww. ja va 2 s.  c o m*/
        log.warn("At least Java 1.5 is required for WebMail.");
    }

    /* Test if the operating system is supported */
    log.info("Operating System: " + System.getProperty("os.name") + "/" + System.getProperty("os.arch") + " "
            + System.getProperty("os.version") + " ... ");
    if (System.getProperty("os.name").equals("SunOS") || System.getProperty("os.name").equals("Solaris")
            || System.getProperty("os.name").equals("Linux")) {
        log.info("OS variant Ok");
    } else {
        log.warn(
                "WebMail was only tested\n   on Solaris, HP-UX and Linux and may cause problems on your platform.");
    }

    /* Check if we are running as root and issue a warning */
    try {
        log.info("User name: " + System.getProperty("user.name") + " ... ");
        if (!System.getProperty("user.name").equals("root")) {
            log.info("User ok.");
        } else {
            log.warn("warning. You are running WebMail as root. This may be a potential security problem.");
        }
    } catch (Exception ex) {
        // Security restriction prohibit reading the username, then we do not need to
        // check for root anyway
    }

    /* Check whether all WebMail system properties are defined */
    log.info("WebMail System Properties: ");
    //checkPathProperty(parent,"webmail.plugin.path");
    //checkPathProperty(parent,"webmail.auth.path");
    checkPathProperty(parent, "webmail.lib.path");
    checkPathProperty(parent, "webmail.template.path");
    checkPathProperty(parent, "webmail.data.path");
    checkPathProperty(parent, "webmail.xml.path");
    log.info("WebMail System Properties ok!");

    log.info("Setting DTD-path in webmail.xml ... ");
    File f1 = new File(
            parent.getProperty("webmail.data.path") + System.getProperty("file.separator") + "webmail.xml");
    File f2 = new File(parent.getProperty("webmail.data.path") + System.getProperty("file.separator")
            + "webmail.xml." + Long.toHexString(System.currentTimeMillis()));

    try {
        Pattern regexp = Pattern.compile("<!DOCTYPE SYSDATA SYSTEM \".*\">");
        BufferedReader file1 = new BufferedReader(new FileReader(f1));
        PrintWriter file2 = new PrintWriter(new FileWriter(f2));
        try {
            String line = file1.readLine();
            while (line != null) {
                Matcher m = regexp.matcher(line);
                String s = m.replaceAll(
                        "<!DOCTYPE SYSDATA SYSTEM \"file://" + parent.getProperty("webmail.xml.path")
                                + System.getProperty("file.separator") + "sysdata.dtd" + "\">");
                //                  String s=regexp.substituteAll(line,"<!DOCTYPE SYSDATA SYSTEM \"file://"+
                //                                                parent.getProperty("webmail.xml.path")+
                //                                                System.getProperty("file.separator")+
                //                                                "sysdata.dtd"+"\">");
                //log.debug(s);
                file2.println(s);
                line = file1.readLine();
            }
        } catch (EOFException ex) {
        }
        file2.close();
        file1.close();
    } catch (Exception ex) {
        throw new WebMailException(ex);
    }
    f2.renameTo(f1);
    log.info("Done checking system!");
}

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

protected void innerProcess(CrawlURI curi) throws InterruptedException {
    // Ok, if we got this far we need to calculate the content digest. 
    // Get the regex
    String regex = getStripRegex();

    // Get a replay of the document character seq.
    ReplayCharSequence cs = null;/*from  w  w  w.jav  a 2  s  . c o m*/
    try {
        cs = curi.getRecorder().getContentReplayCharSequence();
        // Create a MessageDigest 
        MessageDigest digest = null;
        try {
            digest = MessageDigest.getInstance(SHA1);
        } catch (NoSuchAlgorithmException e1) {
            e1.printStackTrace();
            return;
        }

        digest.reset();

        String s = null;

        if (StringUtils.isEmpty(regex)) {
            s = cs.toString();
        } else {
            // Process the document
            Matcher m = TextUtils.getMatcher(regex, cs);
            s = m.replaceAll(" ");
            TextUtils.recycleMatcher(m);
        }
        digest.update(s.getBytes());
        // Get the new digest value
        byte[] newDigestValue = digest.digest();
        // Save new digest value
        curi.setContentDigest(SHA1, newDigestValue);

    } catch (Exception e) {
        curi.getNonFatalFailures().add(e);
        logger.warning("Failed get of replay char sequence " + curi.toString() + " " + e.getMessage() + " "
                + Thread.currentThread().getName());
        return; // Can't proceed if this happens.
    }
}

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

private String removeSpans(String in) {
    Matcher m = spans.matcher(in);
    return m.replaceAll("");
}

From source file:edu.internet2.middleware.psp.ldap.LdapSpmlTarget.java

/**
 * Remove the escape character "\" from all escaped forward slashes "\/", returning "/".
 * /*from   w  w w.  j  av a2s. c om*/
 * @param dn the ldap dn
 * @return the resultant string
 */
public static String unescapeForwardSlash(final String dn) {

    Matcher matcher = escapedforwardSlashPattern.matcher(dn);

    if (matcher.find()) {
        return matcher.replaceAll("/");
    }

    return dn;
}

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

/**
 * Remove <a name=*></a> stuff google inserts everywhere
 *///from w w w  . java2 s .c  o m
private String removeEmptyATags(String in) {
    Matcher m = aTags.matcher(in);
    return m.replaceAll("");
}

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

/**
 * Removes some stuff from the lists// w ww  .  j  av  a  2s. c  o  m
 */
private String removeListAttributes(String in) {
    Matcher m = lists.matcher(in);
    return m.replaceAll("");
}

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

private String removeClasses(String in) {
    Matcher m = classes.matcher(in);
    return m.replaceAll("");
}