Preg_Split number and delim line

I have a problem with preg_split

. I need a regex to split my string into a number and a character. Example of my line:

1_AB_CD_2_ABC_3_ABD

      

and I want the result to be split:

1
AB_CD
2
ABC
3
ABD

      

I've tried with this regex, but this one doesn't work:

 preg_split("/(^\d)(?=_)|(?<=_)(\d)(?=_)/",$sequence,PREG_SPLIT_DELIM_CAPTURE).

      

+3


source to share


1 answer


(?<=\d)_(?=[A-Z0-9]{2})|(?<=[A-Z0-9]{2})_(?=\d)

      

Try it. Check out the demo.



https://regex101.com/r/uE3cC4/26

$returnValue = preg_split('/(?<=\\d)_(?=[A-Z0-9]{2})|(?<=[A-Z0-9]{2})_(?=\\d)/', '1_AB_CD_2_ABC_3_ABD', -1, PREG_SPLIT_NO_EMPTY);

      

+1


source







All Articles