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

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

Description

Splits a string given a pattern (regex), considering escapes.

License

Apache License

Declaration

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

Method Source Code

//package com.java2s;
/****************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one   *
 * or more contributor license agreements.  See the NOTICE file *
 * distributed with this work for additional information        *
 * regarding copyright ownership.  The ASF licenses this file   *
 * to you 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;

public class Main {
    /**/*from w  ww  .j  a v  a  2s .co  m*/
     * Splits a string given a pattern (regex), considering escapes.
     * <p> For example considering a pattern "," we have:
     * one,two,three => {one},{two},{three}
     * one\,two\\,three => {one,two\\},{three}
     * <p>
     * NOTE: Untested with pattern regex as pattern and untested for escape chars in text or pattern.
     */
    public static String[] split(String text, String pattern) {
        String[] array = text.split(pattern, -1);
        ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < array.length; i++) {
            boolean escaped = false;
            if (i > 0 && array[i - 1].endsWith("\\")) {
                // When the number of trailing "\" is odd then there was no separator and this pattern is part of
                // the previous match.
                int depth = 1;
                while (depth < array[i - 1].length()
                        && array[i - 1].charAt(array[i - 1].length() - 1 - depth) == '\\')
                    depth++;
                escaped = depth % 2 == 1;
            }
            if (!escaped)
                list.add(array[i]);
            else {
                String prev = list.remove(list.size() - 1);
                list.add(prev.substring(0, prev.length() - 1) + pattern + array[i]);
            }
        }
        return list.toArray(new String[list.size()]);
    }
}

Related

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