正则表达式按指定长度切割字符串

来源:百度知道 编辑:UC知道 时间:2024/05/16 02:05:24
我想将长度为10的字符串,按照正则表达式,分割成1,3,2,4的段。
例如:abcdefghij 这样一个源字符串,分割为a , bcd , ef , ghij
这样的四段
其中源字符串不一定都是字母,以上只是一个示例。
所需的正则表达式用在php或者javascript中,最好给出相应的函数,以及相应的说明,谢谢!
一楼的回答不管用,请回答者补充一些说明吧。

PHP 代码:

$str="abcdefghij";
if (preg_match("/(.)(...)(..)(....)/",$str,$matches)) {
$str1=$matches[1];
$str2=$matches[2];
$str3=$matches[3];
$str4=$matches[4];
}
加了打印语句:
print '('.$str1.')';
print '('.$str2.')';
print '('.$str3.')';
print '('.$str4.')';

测试结果:
(a)(bcd)(ef)(ghij)

不知是不是版本问题?我的php版本是4.4.9