例子1

从非字母中间提取出字母。并且把字符串分成3截。

public class SpiltText {

    private static Pattern GET_EN_PATTERN = Pattern.compile("[^a-zA-Z]*([a-zA-Z]+)[^a-zA-Z]*");

    public static void main(String[] args) {
        trySplit("");
        trySplit("RustFisher");
        trySplit("..Rust");
        trySplit("Fisher..");
        trySplit("..<<Hello>>..");
    }

    private static String[] splitText(String text) {
        String[] res = new String[3];
        String en = null;
        Matcher matcher = GET_EN_PATTERN.matcher(text);
        while (matcher.find()) {
            en = matcher.group(1);
            res[1] = en;
        }

        if (en != null && en.length() > 0) {
            int markLeftEnd = text.indexOf(en);
            if (markLeftEnd > 0) {
                res[0] = text.substring(0, markLeftEnd);
            }
            if (markLeftEnd + en.length() < text.length()) {
                res[2] = text.substring(markLeftEnd + en.length());
            }
        } else {
            res[0] = text;
        }
        return res;
    }

    private static void trySplit(String s) {
        String[] ret = splitText(s);
        System.out.print(s + "  -->  ");
        for (int i = 0; i < ret.length; i++) {
            System.out.print(ret[i]);
            if (i + 1 < ret.length) {
                System.out.print("  ||  ");
            }
        }
        System.out.println();
    }

}

运行结果

  -->    ||  null  ||  null
RustFisher  -->  null  ||  RustFisher  ||  null
..Rust  -->  ..  ||  Rust  ||  null
Fisher..  -->  null  ||  Fisher  ||  ..
..<<Hello>>..  -->  ..<<  ||  Hello  ||  >>..