【问题标题】:How to make this weird string explode in PHP?如何让这个奇怪的字符串在 PHP 中爆炸?
【发布时间】:2016-04-15 11:24:47
【问题描述】:

我有如下字符串

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 可以完成这项工作。但是您现在可以帮忙处理正则表达式吗?

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

(?!\]\-)\-

【问题讨论】:

  • 看起来像是preg_split() 的工作。
  • 字符串是由固定长度的组组成的吗?因此,第一列(在您的示例中为 DAS)是否总是前三个字符?会后18个字符等吗?
  • @axiac 嗯,我不知道这个功能。我会尝试并再次回来:)
  • @fiprojects 不幸的是:(每个部分的长度也可以变化。
  • 什么决定了第 1、2 和 3 组之间的分隔符?第一个和第二个之间的差距是“-”字符吗? 2nd 和 3rd 之间的差距是方括号外的“-”吗?你是如何确定格式的?您的问题需要更好地澄清这一点,否则这里的人们只会在吸管。

标签: php regex grep


【解决方案1】:

代码:

$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 位置有一个额外的空值。之所以出现,是因为在 regex 末尾放置了零或一重复量词 (?)。量词是必需的,因为没有它,最后一块(索引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 ('-')

【讨论】:

    【解决方案2】:

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

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

    Here is an example:

    $regex = '/(?:^|-)([^-\[]*(?:\[[^\]]+\])?)/';
    $tests = array(
        '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'
    );
    foreach ($tests as $test) {
        preg_match_all($regex, $test, $result);
        print_r($result[1]);
    }
    

    输出:

    // 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
    )
    

    【讨论】:

    • 我在preg_match 和preg_split 中都使用了你的模式,但没有一个有效:(。无论如何,谢谢你的尝试:)
    • @MerianosNikos 我的 PHP 有点生疏了。我会在更多测试后修改答案。
    【解决方案3】:

    这种情况非常适合(*SKIP)(*FAIL) 方法。您想在连字符上拆分字符串,只要它们不在方括号内。

    简单。只需像这样取消这些连字符作为分隔符的资格:

    模式:~\[[^]]+\](*SKIP)(*FAIL)|-~ (Pattern Demo)

    代码:(Demo)

    $strings=['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'];
    
    foreach($strings as $string){
        var_export(preg_split('~\[[^]]+\](*SKIP)(*FAIL)|-~',$string));
        echo "\n\n";
    }
    

    输出:

    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',
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-26
      • 2011-06-03
      • 1970-01-01
      • 2011-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多