Java String Tokenize removeMatchingRegex(String regex, String replacement, String[] tokens, boolean removeEmpty)

Here you can find the source of removeMatchingRegex(String regex, String replacement, String[] tokens, boolean removeEmpty)

Description

Applies given regex on tokens and may optionally delete when a token gets empty.

License

Apache License

Declaration

public static String[] removeMatchingRegex(String regex, String replacement, String[] tokens,
        boolean removeEmpty) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;

public class Main {
    /**//  w  ww .  j  av  a2s  .c  o  m
       * Applies given regex on tokens and may optionally delete when a token gets
       * empty.
       */
    public static String[] removeMatchingRegex(String regex, String replacement, String[] tokens,
            boolean removeEmpty) {
        String[] tk = new String[tokens.length];
        for (int i = 0; i < tokens.length; i++) {
            tk[i] = tokens[i].replaceAll(regex, replacement);
        }
        if (removeEmpty) {
            tk = removeEmpty(tk);
        }
        return tk;
    }

    /**
       * Removes empty tokens from given array. The empty slots will be filled with
       * the follow-up tokens.
       */
    public static String[] removeEmpty(String[] arr) {
        ArrayList<String> list = new ArrayList<>();
        for (String s : arr) {
            if (s != null && !s.isEmpty())
                list.add(s);
        }
        return list.toArray(new String[list.size()]);
    }
}

Related

  1. maxTokenLength(String s)
  2. parseNMTokens(String nmTokens)
  3. parseString(String in, String token)
  4. readTokens(String line)
  5. removeEmptyStrings(String[] tokens)
  6. removeStopWords(String[] tokens, Set stopWords)
  7. stringTokenizer(String in)
  8. substringsBetween(final String str, final String open, final String close, boolean tokenReservedFlag)
  9. tokenArray(StringTokenizer st)