Regular exp = (Digits)*(A|B|DF|XY)+(Digits)+
I'm confused about this pattern really I want to separate this string in PHP, someone can help me My input maybe something like this
and convert to this
Array
(
[0] => 12
[1] => XY
[2] => 1234567890
)
<?php
$input = "12 XY 123456789";
print_r(preg_split('/\d*[(A|B|DF|XY)+\d+]+/', $input, 3));
//print_r(preg_split('/[\s,]+/', $input, 3));
//print_r(preg_split('/\d*[\s,](A|B)+[\s,]\d+/', $input, 3));
You may match and capture the numbers, letters, and numbers:
$input = "12 XY 123456789";
if (preg_match('/^(?:(\d+)\s*)?(A|B|DF|XY)(?:\s*(\d+))?$/', $input, $matches)){
array_shift($matches);
print_r($matches);
}
See the PHP demo and the regex demo.
^ - start of string(?:(\d+)\s*)? - an optional sequence of:
(\d+) - Group 1: any or more digits\s* - 0+ whitespaces(A|B|DF|XY) - Group 2: A, B, DF or XY(?:\s*(\d+))? - an optional sequence of:
\s* - 0+ whitespaces(\d+) - Group 3: any or more digits$ - end of string.