Java Utililty Methods String Extract

List of utility methods to do String Extract

Description

The list of methods to do String Extract are organized into topic(s).

Method

Listextract(final String grammarString, final char separator)
Breaks a grammar string into its parts and returns those.
String grammar = grammarString;
List<Integer> separatorIndices = findSeparatorIndices(grammar, separator);
ArrayList<String> extractedStrings = new ArrayList<>();
int indexBegin = 0;
for (int sepIndex : separatorIndices) {
    extractedStrings.add(grammar.substring(indexBegin, sepIndex));
    indexBegin = sepIndex + 1;
extractedStrings.add(grammar.substring(indexBegin));
return extractedStrings;
Propertiesextract(Properties p, String prefix)
Returns another Properties object only containing the keys from the this object that start with the specified prefix.
Properties subset = null;
if (prefix == null || prefix.length() == 0) {
    if (p.size() > 0)
        subset = p;
} else if (!p.isEmpty()) {
    Iterator itr = p.entrySet().iterator();
    int length = prefix.length();
    while (itr.hasNext()) {
...
String[]extract_tokens(String in_string, String delimiters)
Produce an array of string "tokens" that are present in the specified string and separated by the specified delimiters.
StringTokenizer tokenizer = new StringTokenizer(in_string, delimiters);
Vector tokens = new Vector();
while (tokenizer.hasMoreTokens())
    tokens.add(tokenizer.nextToken());
String list[] = new String[tokens.size()];
for (int i = 0; i < list.length; i++)
    list[i] = (String) tokens.elementAt(i);
return list;
...
CollectionextractAggregationPrimKeysFromSql(final String sql)
Extracts aggregation-definition-prim-keys out of the table-names of the given sql.
final Collection<String> primKeys = new ArrayList<String>();
if (sql != null) {
    String workSql = sql.replaceAll("\\s+", " ");
    workSql = workSql.replaceAll("\\s+", " ");
    boolean condition = false;
    if (workSql.matches("(?i).* (where|order by|group by) .*")) {
        condition = true;
    final String fromClause = condition
            ? workSql.replaceFirst("(?i).*?from(.*?)(where|order by|group by).*", "$1")
            : workSql.replaceFirst("(?i).*?from(.*)", "$1");
    final String[] tables = fromClause.split(",");
    for (String table : tables) {
        if (table.matches(".*?_.*")) {
            table = table.replaceFirst(".*?\\.", "").trim();
            if (table.startsWith("_")) {
                table = table.replaceFirst("_", "");
            primKeys.add(table.replaceFirst("(.*?)_.*", "$1"));
return primKeys;
MapextractArrayData(String input, StringBuilder output)
extract Array Data
Map<String, String> arrayData = new HashMap<>();
boolean inArrayData = false;
int arrStart = -1;
int blockStart = -1;
for (int i = 0, many = input.length(); i < many; i++) {
    char ch = input.charAt(i);
    char nextChar = i < many - 1 ? input.charAt(i + 1) : 0;
    switch (ch) {
...
MapextractArrayData(String input, StringBuilder output)
extract Array Data
Map<String, StringBuilder> arrayData = new HashMap<>();
StringBuilder currentArray = null;
String currentArrayName = null;
boolean inArrayData = false;
char nextChar;
for (int i = 0, many = input.length(); i < many; i++) {
    char ch = Character.toLowerCase(input.charAt(i));
    nextChar = i < many - 1 ? input.charAt(i + 1) : 0;
...
ListextractCharNgram(String content, int n)
Calculates character n-grams from a String.
List<String> charNgram = new ArrayList<String>();
if (content.length() >= n) {
    for (int i = 0; i < content.length() - n; i++) {
        String cgram = "";
        for (int j = i; j < i + n; j++) {
            cgram += content.charAt(j);
        charNgram.add(cgram);
...
ListextractCommands(String fileContents)
extract Commands
List<String> commands = new ArrayList<String>();
int i = 0;
while (i < fileContents.length()) {
    int j = fileContents.indexOf(";", i);
    if (j == -1) {
        j = fileContents.length();
    String command = fileContents.substring(i, j).trim();
...
StringextractData(String message)
extract Data
String parts[] = message.split(" ");
return String.join(" ", Arrays.copyOfRange(parts, 2, parts.length));
StringextractField(int position, String regex, String source)
extract Field
String[] splitted = source.split(regex, position + 2);
return splitted[position];