1

我想用 PHP 中的 preg_match_all 在他们自己的组中捕获这些中的每一个。

  1. 章、节或页
  2. 指定章、节或页的编号(或字母,如果有的话)。如果它们之间有一个空格,则应考虑到
  3. 单词“和”、“或”

请记住,字符串中的项目数可能是动态的,正则表达式应该适用于以下所有示例:

  1. Ch1 和 Sect2b
  2. Ch 4 x blahunwantedtext 和 Sect 5y 和 Sect6 z 和 Ch7 或 Ch8

到目前为止,这是我设法提出的:

<?php

    $str = 'Ch 1 a and Sect 2b and Pg3';
    preg_match_all ('/([a-z]+)([\s]?[0-9]+)([\s]?[a-z]*)([\s]?and*[\s]?)/is', $str, $matches);

    Array
    (
        [0] => Array
            (
                [0] => Ch 1 a and 
                [1] => Sect 2b and 
            )

        [1] => Array
            (
                [0] => Ch
                [1] => Sect
            )

        [2] => Array
            (
                [0] =>  1
                [1] =>  2
            )

        [3] => Array
            (
                [0] =>  a
                [1] => b
            )

        [4] => Array
            (
                [0] =>  and 
                [1] =>  and 
            )

    )

我无法匹配数组中字符串 (Pg3) 的最后部分。

预期的结果应该是:

    Array
    (
        [0] => Array
            (
                [0] => Ch 1 a and 
                [1] => Sect 2b and 
                [2] => Pg3
            )

        [1] => Array
            (
                [0] => Ch
                [1] => Sect
                [2] => Pg
            )

        [2] => Array
            (
                [0] =>  1
                [1] =>  2
                [2] =>  3
            )

        [3] => Array
            (
                [0] =>  a
                [1] => b
                [2] => 
            )

        [4] => Array
            (
                [0] =>  and 
                [1] =>  and 
                [2] =>  
            )

    )
4

1 回答 1

1

这个正则表达式应该工作/(ch|sect|pg)\s*(\d)\s*([a-z]?\b)\s*(and|or)?/i

$str = 'Ch 1 a and Sect 2b and Pg3';
preg_match_all('/(ch|sect|pg)\s*(\d)\s*([a-z]?\b)\s*(and|or)?/i', $str, $matches);


array (size=5)
  0 => 
    array (size=3)
      0 => string 'Ch 1 a and' (length=10)
      1 => string 'Sect 2b and' (length=11)
      2 => string 'Pg3' (length=3)
  1 => 
    array (size=3)
      0 => string 'Ch' (length=2)
      1 => string 'Sect' (length=4)
      2 => string 'Pg' (length=2)
  2 => 
    array (size=3)
      0 => string '1' (length=1)
      1 => string '2' (length=1)
      2 => string '3' (length=1)
  3 => 
    array (size=3)
      0 => string 'a' (length=1)
      1 => string 'b' (length=1)
      2 => string '' (length=0)
  4 => 
    array (size=3)
      0 => string 'and' (length=3)
      1 => string 'and' (length=3)
      2 => string '' (length=0)
于 2013-01-13T07:26:47.470 回答