I have a problem with preg_split
. I need a regex to split my string into number and character. An example of my string is:
我有preg_split的问题。我需要一个正则表达式将我的字符串分成数字和字符。我的字符串的一个例子是:
1_AB_CD_2_ABC_3_ABD
and I want a result splitted:
我想要一个分裂的结果:
1
AB_CD
2
ABC
3
ABD
I've tried with this regex expression but this one not work:
我试过这个正则表达式但是这个不起作用:
preg_split("/(^\d)(?=_)|(?<=_)(\d)(?=_)/",$sequence,PREG_SPLIT_DELIM_CAPTURE).
1 个解决方案
#1
(?<=\d)_(?=[A-Z0-9]{2})|(?<=[A-Z0-9]{2})_(?=\d)
Try this.See 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
(?<=\d)_(?=[A-Z0-9]{2})|(?<=[A-Z0-9]{2})_(?=\d)
Try this.See 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);