日期:2014-05-20  浏览次数:20718 次

正则表达式如何把String截取位定长的字符数组?
正则表达式怎么把一个String截取为长20个字符的字符串数组?比如"aaaa..."(40个)截取为长度为20个字符的两个字符串?

------解决方案--------------------
Java code


  regex = "\\w{20}"  
    String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567";
    Matcher matcher =Pattern.compile(regex).matcher(str);
    while(matcher.find()) System.out.println(Arrays.toString(  matcher.group().toCharArray()  ));
    /*
        [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t]
    [u, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N]
        [O, P, Q, R, S, T, U, V, W, X, Y, Z, 0, 1, 2, 3, 4, 5, 6, 7]
     */