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.salesmanager.core.util.CreditCardUtil.java

public void validate(String number, int type, String month, String date) throws CreditCardUtilException {

    try {/*from  w  ww .jav  a  2  s. c  o m*/
        Integer.parseInt(month);
        Integer.parseInt(date);
    } catch (NumberFormatException nfe) {
        throw new CreditCardUtilException(LabelUtil.getInstance().getText("errors.creditcard.invaliddate"),
                CreditCardUtilException.DATE);
    }

    if (number.equals("")) {
        throw new CreditCardUtilException(LabelUtil.getInstance().getText("errors.creditcard.invalidnumber"));
    }

    Matcher m = Pattern.compile("[^\\d\\s.-]").matcher(number);

    if (m.find()) {
        // setMessage("Credit card number can only contain numbers, spaces, \"-\", and \".\"");
        throw new CreditCardUtilException(LabelUtil.getInstance().getText("errors.creditcard.invalidnumber"));
    }

    Matcher matcher = Pattern.compile("[\\s.-]").matcher(number);

    number = matcher.replaceAll("");
    validateDate(Integer.parseInt(month), Integer.parseInt(date));
    validateNumber(number, type);
}

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

/**
 * Iterates through the acronym properties and replaces matching
 * acronyms in the entry text with acronym html-tags.
 *
 * @param text entry text/*from  www.j a va  2s  . c  o m*/
 * @param acronyms user provided set of acronyms
 * @return entry text with acronym explanations
 */
private String matchAcronyms(String text, Pattern[] acronymPatterns, String[] acronymTags) {
    if (mLogger.isDebugEnabled()) {
        mLogger.debug("matchAcronyms(" + text + ")");
    }

    Matcher matcher = null;
    for (int i = 0; i < acronymPatterns.length; i++) {
        matcher = acronymPatterns[i].matcher(text);
        text = matcher.replaceAll(acronymTags[i]);
    }
    return text;
}

From source file:org.cartoweb.stats.report.Reports.java

private String toDbName(String value) {
    Matcher matcher = TRANSFORM.matcher(value);
    return matcher.replaceAll("_");
}

From source file:org.lanes.text.mining.Conceptualiser.java

public List<String> findNeighbours(String concept, String type, int iteration) {
    DatabaseConnection dbase = new DatabaseConnection();
    Connection conn = dbase.establishConnection("lanes");

    List<String> neighbours = new ArrayList<String>();

    //System.err.println("find-neighbours[" + concept + "][" + type + "]");

    Matcher replace = Pattern.compile("\\s").matcher(concept);
    concept = replace.replaceAll("_");

    try {// www .  j av  a  2 s . c  o m
        PreparedStatement statement1 = conn
                .prepareStatement("SELECT arg2 FROM category WHERE arg1 = ? AND reltype = ?");
        statement1.setString(1, concept);
        statement1.setString(2, type);
        ResultSet rs1 = statement1.executeQuery();
        while (rs1.next()) {
            String neighbour = rs1.getString(1);
            neighbours.add(neighbour);
        }
        rs1.close();
        statement1.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    dbase.closeConnection(conn);

    if (neighbours.size() == 0 && !type.equals("SYNONYMY") && iteration == 0) {
        List<String> synonyms = findNeighbours(concept, "SYNONYMY");
        iteration++;
        for (String synonym : synonyms) {
            List<String> neighs = findNeighbours(synonym, type, iteration);
            neighbours.addAll(neighs);
        }
    }

    return neighbours;
}

From source file:com.fujitsu.dc.test.jersey.ODataCommon.java

/**
 * Etag???./*from w  w w .j  a v a  2 s. c  o  m*/
 * @param etag Etag
 * @return ?
 */
public static long getEtagVersion(String etag) {
    // version?
    Pattern pattern = Pattern.compile("^W/\"([0-9]+)-([0-9]+)\"$");
    Matcher m = pattern.matcher(etag);
    return Long.parseLong(m.replaceAll("$1"));
}

From source file:com.fujitsu.dc.test.jersey.ODataCommon.java

/**
 * Etag?Updated?./*from w  ww  .  ja va2s .c o  m*/
 * @param etag Etag
 * @return Updated?
 */
public static long getEtagUpdated(String etag) {
    // version?
    Pattern pattern = Pattern.compile("^W/\"([0-9]+)-([0-9]+)\"$");
    Matcher m = pattern.matcher(etag);
    return Long.parseLong(m.replaceAll("$2"));
}

From source file:Normalization.TextNormalization.java

public String removeEmojiFromString(String content) {

    String utf8tweet = "";
    try {/*from w  w w . j  av  a2 s  .  com*/
        byte[] utf8Bytes = content.getBytes("UTF-8");

        utf8tweet = new String(utf8Bytes, "UTF-8");
    } catch (UnsupportedEncodingException e) {
    }
    Pattern unicodeOutliers = Pattern.compile(
            "[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
            Pattern.UNICODE_CASE | Pattern.CANON_EQ | Pattern.CASE_INSENSITIVE);
    Matcher unicodeOutlierMatcher = unicodeOutliers.matcher(utf8tweet);

    utf8tweet = unicodeOutlierMatcher.replaceAll("");
    return utf8tweet;
}

From source file:Normalization.TextNormalization.java

public String removeMentionsFromString(String content) {

    String utf8tweet = "";
    try {/*from   w ww  .j  a v a2 s  .co m*/
        byte[] utf8Bytes = content.getBytes("UTF-8");

        utf8tweet = new String(utf8Bytes, "UTF-8");
    } catch (UnsupportedEncodingException e) {
    }

    final String regex = "[@]\\w+";
    final Pattern unicodeOutliers = Pattern.compile(regex,
            Pattern.MULTILINE | Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);

    Matcher unicodeOutlierMatcher = unicodeOutliers.matcher(utf8tweet);
    utf8tweet = unicodeOutlierMatcher.replaceAll("");
    return utf8tweet.replace("#", "");
}

From source file:Normalization.TextNormalization.java

public String removeUrlsFromString(String content) {

    String utf8tweet = "";
    try {/* w  w  w.j  ava  2  s . c  om*/
        byte[] utf8Bytes = content.getBytes("UTF-8");

        utf8tweet = new String(utf8Bytes, "UTF-8");
    } catch (UnsupportedEncodingException e) {
    }

    final String regex = "(https?|ftp|file|pic|www)[:|.][-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|]";
    final Pattern unicodeOutliers = Pattern.compile(regex,
            Pattern.MULTILINE | Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);

    Matcher unicodeOutlierMatcher = unicodeOutliers.matcher(utf8tweet);
    utf8tweet = unicodeOutlierMatcher.replaceAll("");
    return utf8tweet;
}

From source file:Normalization.TextNormalization.java

public String removeSymbolsFromString(String content) {

    String utf8tweet = "";
    try {/*w  w  w  .ja va 2s .  c o m*/
        byte[] utf8Bytes = content.getBytes("UTF-8");

        utf8tweet = new String(utf8Bytes, "UTF-8");
    } catch (UnsupportedEncodingException e) {
    }

    final String regex = "[\\./\\()\"':,.;<>~!$%^&*\\|+={}?\\-`1234567890_]";
    final Pattern unicodeOutliers = Pattern.compile(regex,
            Pattern.MULTILINE | Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);

    Matcher unicodeOutlierMatcher = unicodeOutliers.matcher(utf8tweet);
    utf8tweet = unicodeOutlierMatcher.replaceAll(" ");
    return utf8tweet;
}