Java String Split by Regex splitTerms(String text, String pattern)

Here you can find the source of splitTerms(String text, String pattern)

Description

Splits the passed string around matches of the given pattern.

License

Open Source License

Parameter

Parameter Description
text The string to split.
pattern The delimiting regular expression.

Return

See above.

Declaration

public static List<String> splitTerms(String text, String pattern) 

Method Source Code


//package com.java2s;
/*/*from  w  ww  .  j av  a 2  s .co  m*/
 * org.openmicroscopy.shoola.util.ui.search.SearchUtil 
 *
 *------------------------------------------------------------------------------
 *  Copyright (C) 2006-2007 University of Dundee. All rights reserved.
 *
 *
 *    This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *------------------------------------------------------------------------------
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    /** Separator between words. */
    public static final String COMMA_SEPARATOR = ",";
    /** The separator between the first name and the last name. */
    public static final String SPACE_SEPARATOR = " ";
    /** The separator between the first name and the last name. */
    public static final String QUOTE_SEPARATOR = "\"";
    /** The default text value. */
    static final String ALL = "All";

    /**
     * Splits the passed string around matches of the given pattern.
     * Returns a list of elements
     * 
     * @param text      The string to split.
     * @param pattern   The delimiting regular expression.
     * @return See above.
     */
    public static List<String> splitTerms(String text, String pattern) {
        List<String> l = new ArrayList<String>();
        if (text == null)
            return l;
        text = text.trim();
        String[] r = text.split(pattern);
        String value;
        for (int i = 0; i < r.length; i++) {
            value = r[i];
            if (value != null) {
                value = value.trim();
                if (value.length() != 0 && !value.equals(ALL))
                    l.add(value);
            }
        }
        return l;
    }

    /**
     * Splits the passed string around matches of the given pattern.
     * Returns a list of elements
     * 
     * @param text      The string to split.
     * @return See above.
     */
    public static List<String> splitTerms(String text) {
        List<String> l = new ArrayList<String>();
        if (text == null)
            return l;
        text = text.trim();
        String pattern = SPACE_SEPARATOR;
        if (text.contains(QUOTE_SEPARATOR))
            pattern = QUOTE_SEPARATOR;
        String[] r = text.split(pattern);
        String value;
        for (int i = 0; i < r.length; i++) {
            value = r[i];
            if (value != null) {
                value = value.trim();
                if (value.length() != 0 && !value.equals(ALL) && !value.equals(COMMA_SEPARATOR)) {
                    if (value.contains(","))
                        value = value.replace(",", "");
                    l.add(value);
                }
            }
        }
        return l;
    }
}

Related

  1. splitAndTrim(String s, String regex)
  2. splitList(String list, String splitRegex)
  3. splitNoEmpty(String sStr, String regex)
  4. splitString(String str, List list, String regex)
  5. splitStringToList(String input, String regex)
  6. splitToList(String s, String regex)
  7. splitToLongList(String str, String regex)