示例输入:
hjkhwe5boijdfg
我需要将其拆分为3个变量,如下所示:
我有PHP preg_match
所有设置,但不知道如何做这个复杂的正则表达式。有人可以帮我一把吗?
答案 0 :(得分:1)
尝试使用:
$str = 'hjkhwe5boijdfg';
preg_match("/^([a-z]+\d+)([a-z])(.*)$/", $str, $m);
print_r($m);
<强>输出:强>
Array
(
[0] => hjkhwe5boijdfg
[1] => hjkhwe5
[2] => b
[3] => oijdfg
)
<强>解释强>
^ : begining of line
( : 1rst group
[a-z]+ : 1 or more letters
\d+ : followed by 1 or more digit
) : end of group 1
( : 2nd group
[a-z] : 1 letter
) : end group 2
( : 3rd group
.* : any number of any char
) : end group 3
$
答案 1 :(得分:0)
您可以将preg_match用作:
$str = 'hjkhwe5boijdfg';
if(preg_match('/^(\D*\d+)(\w)(.*)$/',$str,$m)) {
// $m[1] has part 1, $m[2] has part 2 and $m[3] has part 3.
}