2013-01-13 79 views
1

我想用PHP中的preg_match_all在每個組中捕獲它們中的每一個。PHP preg-match-all正則表達式

  1. 的章,節,或頁面
  2. 的數字(或字母,如果它有一個)將指定的章,節,或網頁的。如果他們之間有一個空格應該考慮到
  3. 詞「與」,「或」

牢記字符串中的項目數量可以是動態的,正則表達式應該

  1. Ch1和Sect2b
  2. 章4×blahunwantedtext和派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] => 
      ) 

    ) 
+1

添加一些例子,您的文章。擁有「輸入 - >預期結果」可以更輕鬆地計算出100倍的問題。 – Supericy

+0

@Supericy預期結果已添加。 – user1307016

+0

@Supericy只是想知道如果有一些額外的不需要的文本,比如$ str ='Ch 1 a blahblahdontwant和Sect 2b和Pg3',我需要更改爲正則表達式以獲得相同的結果嗎? – user1307016

回答

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)