Java String Split by Regex split(String string, String pattern)

Here you can find the source of split(String string, String pattern)

Description

Split a string on pattern, making sure that we do not split on quoted sections.

License

Apache License

Parameter

Parameter Description
string the string to be split
pattern the characters to split on

Return

an array if split sections

Declaration

public static String[] split(String string, String pattern) 

Method Source Code

//package com.java2s;
/**//from w w w  . j  ava 2  s.c  o m
 *
 * Copyright 2007-2009 (C) The original author or authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Split a string on <i>pattern</i>, making sure that we do not split
     * on quoted sections.
     *
     * @param string  the string to be split
     * @param pattern the characters to split on
     * @return an array if split sections
     */
    public static String[] split(String string, String pattern) {
        assert string != null;
        assert pattern != null;

        List<String> list = new ArrayList<String>(1);
        StringBuilder builder = new StringBuilder();
        boolean inQuotes = false;

        for (int i = 0; i < string.length(); i++) {
            if (!inQuotes && match(pattern, string, i)) {
                list.add(builder.toString());
                builder = new StringBuilder();
                i += pattern.length() - 1;
            } else if (string.charAt(i) == '"') {
                inQuotes = !inQuotes;
                builder.append(string.charAt(i));
            } else {
                builder.append(string.charAt(i));
            }
        }

        list.add(builder.toString());

        return list.toArray(new String[list.size()]);
    }

    public static boolean match(String pattern, String string, int start) {
        assert pattern != null;
        assert string != null;

        int j = 0;
        for (int i = start; i < string.length() && j < pattern.length(); i++, j++) {
            if (pattern.charAt(j) != string.charAt(i))
                return false;
        }
        return j == pattern.length();
    }

    public static boolean match(Object value, String test) {
        if (value instanceof String) {
            return test.equals(value);
        } else if (value instanceof String[])
            return match((String[]) value, test);
        return false;
    }

    public static boolean match(String[] values, String test) {
        if (values.length == 1)
            return values[0].length() == 0 || test.equals(values[0]);

        if (!values[0].regionMatches(0, test, 0, values[0].length()))
            return false;

        int pointer = values[0].length();

        done: for (int i = 1; i < values.length - 1; i++) {
            int length = values[i].length();
            int limit = test.length() - length;
            while (pointer <= limit) {
                if (values[i].regionMatches(0, test, pointer++, length)) {
                    pointer += values[i].length() - 1;
                    continue done;
                }
            }
            return false;
        }

        return test.substring(pointer).endsWith(values[values.length - 1]);
    }
}

Related

  1. split(final String regex, final String value)
  2. split(final String string, final String regex)
  3. split(String input, String regex)
  4. split(String str, String regex)
  5. split(String str, String regex)
  6. split(String text, String pattern)
  7. splitAndTrim(String s, String regex)
  8. splitList(String list, String splitRegex)
  9. splitNoEmpty(String sStr, String regex)