-1

I have a series of strings in the form of

10a99b5c
55
2a3b1
g

How can I split the string by the characters (which always appears as a single character between numbers or in the beginning/end of the string)

array([0] => 10a99b5c, [1] => 10, [2]=> a99, [3] => b5, [4] => c),
array([0] => 55, [1] => 55),
array([0] => 2a3b1, [1] => 2, [2] => a3, [3] => b1),
array([0] => g, [1] => g),

The output format is not important, I can handle, I just have no idea what sort of regex pattern can do the trick (even a hint is sufficient).

I do this with preg_replace_callback where I use the digits found for each character for a set of calculations.

5
  • Could you add what you've tried so far ? Commented Jan 28, 2020 at 18:02
  • @Nicolas as I mentioned, I am looking for a hint to begin with. I have no clue what can match the existing and missing elements. Commented Jan 28, 2020 at 18:03
  • The hint is: regexr.com 😁 Commented Jan 28, 2020 at 18:07
  • 1
    another hint: regex101.com/r/Ox4yUh/1 Commented Jan 28, 2020 at 18:09
  • @Luuk Wonderful, I got what I needed. If you post it as an answer, an accepted answer may help other people too. Thanks! Commented Jan 28, 2020 at 18:12

3 Answers 3

1

Try this:

$input = ['10a99b5c','55','2a3b1','g'];
$regEx = '~([a-z]\d*)~';

foreach($input as $str){
  $res[] = preg_split($regEx,$str,-1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
}
echo '<pre>';
var_export($res);

The result for the examples above:

array (
  0 => 
  array (
    0 => '10',
    1 => 'a99',
    2 => 'b5',
    3 => 'c',
  ),
  1 => 
  array (
    0 => '55',
  ),
  2 => 
  array (
    0 => '2',
    1 => 'a3',
    2 => 'b1',
  ),
  3 => 
  array (
    0 => 'g',
  ),
) 
Sign up to request clarification or add additional context in comments.

Comments

1

The regular expression:

/[a-z]*[0-9]*/

  • [a-z] will match any (lower-case) character
    • zero, or more times
  • [0-9] will match a number
    • zero, or more times

https://regex101.com/r/Ox4yUh/1

2 Comments

Please note that this regex will also match aaaaaaa10 i'm not sure if that's what you were looking for
This Googlebot is only interested in an answer, not in the why...., so who cares... 😁 But indeed the first '*' should be a '?'.
0

Your task is simple, split on the zero-width position after each sequence of numbers.

$array = preg_split('/\d+\K/', $string, 0, PREG_SPLIT_NO_EMPTY);

Match the digits with \d+, then release them with \K so that the numbers are not "consumed" during the explosion.

I'll bundle your four sample strings into an array of test strings.

Code: (Demo)

$strings = [
    '10a99b5c',
    '55',
    '2a3b1',
    'g',
];

var_export(
    array_map(
        fn($string) => preg_split('/\d+\K/', $string, 0, PREG_SPLIT_NO_EMPTY),
        $strings
    )
);

Output:

array (
  0 => 
  array (
    0 => '10',
    1 => 'a99',
    2 => 'b5',
    3 => 'c',
  ),
  1 => 
  array (
    0 => '55',
  ),
  2 => 
  array (
    0 => '2',
    1 => 'a3',
    2 => 'b1',
  ),
  3 => 
  array (
    0 => 'g',
  ),
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.