如何使这个奇怪的字符串在PHP中爆炸?

如何使这个奇怪的字符串在PHP中爆炸?,php,regex,grep,Php,Regex,Grep,我有一个如下的字符串 DAS-1111[DR-Helpfull-R]-RUN--[121668688374]-N-[+helpfull_+string] 上面的字符串是一种分组格式,如下所示: A-B[C]-D-E-[F]-G-[H] 我的想法是,我喜欢处理其中的一些群体,我喜欢制作一些类似爆炸的东西 我说喜欢,因为我已经尝试了以下代码: $string = 'DAS-1111[DR-Helpfull-R]-RUN--[121668688374]-N-[+helpfull_+string]'

我有一个如下的字符串

DAS-1111[DR-Helpfull-R]-RUN--[121668688374]-N-[+helpfull_+string]
上面的字符串是一种分组格式,如下所示:

A-B[C]-D-E-[F]-G-[H]
我的想法是,我喜欢处理其中的一些群体,我喜欢制作一些类似爆炸的东西

我说喜欢,因为我已经尝试了以下代码:

$string = 'DAS-1111[DR-Helpfull-R]-RUN--[121668688374]-N-[+helpfull_+string]';
$parts = explode( '-', $string );
print_r( $parts );
我得到以下结果:

Array
(
    [0] => DAS
    [1] => 1111[DR
    [2] => Helpfull
    [3] => R]
    [4] => RUN
    [5] => 
    [6] => [121668688374]
    [7] => N
    [8] => [+helpfull_+string]
)
这不是我需要的

我需要的是以下输出:

Array
(
    [0] => DAS
    [1] => 1111[DR-Helpfull-R]
    [2] => RUN
    [3] => 
    [4] => [121668688374]
    [5] => N
    [6] => [+helpfull_+string]
)
有人能建议一个漂亮而优雅的方法,以我需要的方式引爆这根绳子吗

我忘记提到的是,字符串可以有更多或更少的组。示例:

DAS-1111[DR-Helpfull-R]-RUN--[121668688374]-N-[+helpfull_+string]
DAS-1111[DR-Helpfull-R]-RUN--[121668688374]
DAS-1111[DR-Helpfull-R]-RUN--[121668688374]-N-[+helpfull_+string]-anotherPart
更新1

正如@axiac所提到的,
preg_split
可以完成这项工作。但是你现在能帮我学一下正则表达式吗

我尝试过这个,但似乎不正确:


(?!\]\-)\-

您应该尝试匹配以下模式,而不是爆炸:

(?:^|-)([^-\[]*(?:\[[^\]]+\])?)
:

输出:

// DAS-1111[DR-Helpfull-R]-RUN--[121668688374]-N-[+helpfull_+string]
Array
(
    [0] => DAS
    [1] => 1111[DR-Helpfull-R]
    [2] => RUN
    [3] => 
    [4] => [121668688374]
    [5] => N
    [6] => [+helpfull_+string]
)

// DAS-1111[DR-Helpfull-R]-RUN--[121668688374]
Array
(
    [0] => DAS
    [1] => 1111[DR-Helpfull-R]
    [2] => RUN
    [3] => 
    [4] => [121668688374]
)

// DAS-1111[DR-Helpfull-R]-RUN--[121668688374]-N-[+helpfull_+string]-anotherPart
Array
(
    [0] => DAS
    [1] => 1111[DR-Helpfull-R]
    [2] => RUN
    [3] => 
    [4] => [121668688374]
    [5] => N
    [6] => [+helpfull_+string]
    [7] => anotherPart
)
array (
  0 => 'DAS',
  1 => '1111[DR-Helpfull-R]',
  2 => 'RUN',
  3 => '',
  4 => '[121668688374]',
  5 => 'N',
  6 => '[+helpfull_+string]',
)

array (
  0 => 'DAS',
  1 => '1111[DR-Helpfull-R]',
  2 => 'RUN',
  3 => '',
  4 => '[121668688374]',
)

array (
  0 => 'DAS',
  1 => '1111[DR-Helpfull-R]',
  2 => 'RUN',
  3 => '',
  4 => '[121668688374]',
  5 => 'N',
  6 => '[+helpfull_+string]',
  7 => 'anotherPart',
)
守则:

$str = 'DAS-1111[DR-Helpfull-R]-RUN--[121668688374]-N-[+helpfull_+string]';
$re  = '/([^-[]*(?:\[[^\]]*\])?[^-]*)-?/';

$matches = array();
preg_match_all($re, $str, $matches);
print_r($matches[1]);
其产出:

Array
(
    [0] => DAS
    [1] => 1111[DR-Helpfull-R]
    [2] => RUN
    [3] =>
    [4] => [121668688374]
    [5] => N
    [6] => [+helpfull_+string]
    [7] =>
)
在输出中的位置
7
处有一个额外的空值。它之所以出现,是因为在
正则表达式
末尾有零个或一个重复量词(
)。需要量词,因为没有它,最后一个片段(在索引
6
)不匹配

您可以在最后一个
-
之后删除
,并通过这种方式询问破折号(
-
)是否始终匹配。在这种情况下,必须在输入字符串中附加一个额外的
-

正则表达式

(              # start of the 1st subpattern
               # the captured value is returned in $matches[1]
  [^-[]*       # match any character but '-' and '[', zero or more times
  (?:          # start of a non-capturing subpattern
    \[         # match an opening square bracket ('[')
    [^\]]*     # match any character but ']', zero or more times
    \]         # match a closing square bracket (']')
  )?           # end of the subpattern; it is optional (can appear 0 or 1 times)
  [^-]*        # match any character but '-', zero or more times
)              # end of the 1st subpattern
-?             # match an optional dash ('-')

这种情况非常适合
(*跳过)(*失败)
方法。您希望在连字符上拆分字符串,只要它们不在方括号内

简单。只需取消这些连字符作为分隔符的资格,如下所示:

模式:
~\[[^]]+\](*跳过)(*失败)~
()

代码:()

输出:

// DAS-1111[DR-Helpfull-R]-RUN--[121668688374]-N-[+helpfull_+string]
Array
(
    [0] => DAS
    [1] => 1111[DR-Helpfull-R]
    [2] => RUN
    [3] => 
    [4] => [121668688374]
    [5] => N
    [6] => [+helpfull_+string]
)

// DAS-1111[DR-Helpfull-R]-RUN--[121668688374]
Array
(
    [0] => DAS
    [1] => 1111[DR-Helpfull-R]
    [2] => RUN
    [3] => 
    [4] => [121668688374]
)

// DAS-1111[DR-Helpfull-R]-RUN--[121668688374]-N-[+helpfull_+string]-anotherPart
Array
(
    [0] => DAS
    [1] => 1111[DR-Helpfull-R]
    [2] => RUN
    [3] => 
    [4] => [121668688374]
    [5] => N
    [6] => [+helpfull_+string]
    [7] => anotherPart
)
array (
  0 => 'DAS',
  1 => '1111[DR-Helpfull-R]',
  2 => 'RUN',
  3 => '',
  4 => '[121668688374]',
  5 => 'N',
  6 => '[+helpfull_+string]',
)

array (
  0 => 'DAS',
  1 => '1111[DR-Helpfull-R]',
  2 => 'RUN',
  3 => '',
  4 => '[121668688374]',
)

array (
  0 => 'DAS',
  1 => '1111[DR-Helpfull-R]',
  2 => 'RUN',
  3 => '',
  4 => '[121668688374]',
  5 => 'N',
  6 => '[+helpfull_+string]',
  7 => 'anotherPart',
)

这看起来像是的作业。字符串是由固定长度的组组成的吗?因此,第一列(示例中的DAS)是否总是前三个字符?第二个18个字符等吗?@axiac Mmm我不知道这个函数。我将再次尝试并返回:)@fit不幸的是,不是:(每个部分的长度也可能不同。什么决定了第1、第2和第3组之间的分隔符?第1和第2组之间的间隙是一个'-'字符?第2和第3组之间的间隙是方括号外的'-'字符?你是如何确定格式的?你的问题需要更好地澄清这一点,否则这里的人只会在straws.我使用了你的模式,同时使用了
preg_match
preg_split
,但都不起作用:(.无论如何,谢谢你的尝试:)@MerianosNikos我的PHP有点生疏。我会在进一步测试后修改答案。