2017-10-18 81 views
1

考慮到這個示例文本:如何將變量搜索限制爲單行文本?

grupo1, tiago1A, bola1A, mola1A, tijolo1A, pedro1B, bola1B, mola1B, tijolo1B, raimundo1C, bola1C, mola1C, tijolo1C, joao1D, bola1D, mola1D, tijolo1D, felipe1E, bola1E, mola1E, tijolo1E, 

grupo2, tiago2A, bola2A, mola2A, tijolo2A, pedro2B, bola2B, mola2B, tijolo2B, raimundo2C, bola2C, mola2C, tijolo2C, joao2D, bola2D, mola2D, tijolo2D, felipe2E, bola2E, mola2E, tijolo2E, 

grupo3, tiago3A, bola3A, mola3A, tijolo3A, pedro3B, bola3B, mola3B, tijolo3B, raimundo3C, bola3C, mola3C, tijolo3C, joao3D, bola3D, mola3D, tijolo3D, felipe3E, bola3E, mola3E, tijolo3E, 

grupo4, tiago4A, bola4A, mola4A, tijolo4A, pedro4B, bola4B, mola4B, tijolo4B, raimundo4C, bola4C, mola4C, tijolo4C, joao4D, bola4D, mola4D, tijolo4D, felipe4E, bola4E, mola4E, tijolo4E, 

grupo5, tiago5A, bola5A, mola5A, tijolo5A, pedro5B, bola5B, mola5B, tijolo5B, raimundo5C, bola5C, mola5C, tijolo5C, joao5D, bola5D, mola5D, tijolo5D, felipe5E, bola5E, mola5E, tijolo5E, 

我想捕捉遵循grupo3的20個值,並將其存儲在4

組我使用這樣的:(Demo

/grupo3,((.*?),(.*?),(.*?),(.*?)),/ 

但這隻返回grupo3後面的前4個逗號分隔值。

我需要生成該數組結構:

Match 1 
Group 1 tiago3A 
Group 2 bola3A 
Group 3 mola3A 
Group 4 tijolo3A 

Match 2 
Group 1 pedro3B 
Group 2 bola3B 
Group 3 mola3B 
Group 4 tijolo3B 

Match 3 
Group 1 raimundo3C 
Group 2 bola3C 
Group 3 mola3C 
Group 4 tijolo3C 

Match 4 
Group 1 joao3D 
Group 2 bola3D 
Group 3 mola3D 
Group 4 tijolo3D 

Match 5 
Group 1 felipe3E 
Group 2 bola3E 
Group 3 mola3E 
Group 4 tijolo3E 
+0

爲什麼你之前'有空間*'在質疑你的正則表達式,但不在演示中? – Barmar

+1

你在regex101上做了什麼,你把'grupo3'放在正則表達式的開頭? – Barmar

+0

@Makyen好吧,我已經明白了這個問題。如果問題不再需要進一步編輯,我會通知答案更新他們的工作。 – mickmackusa

回答

1

你可以嘗試以下方法:

/,(.*?),(.*?),(.*?),(.*?),.*?$/m

到底/m指示multi-line$的標誌,指示end of line之前。 Demo

編輯:爲了得到每4個元素只能形成第三段

/grupo3,((.*?),(.*?),(.*?),(.*?)), ((.*?),(.*?),(.*?),(.*?)), ((.*?),(.*?),(.*?),(.*?)), ((.*?),(.*?),(.*?),(.*?)), ((.*?),(.*?),(.*?),(.*?)),/ 

Demo

,你可以得到所需的輸出在PHP中,如:

preg_match('/grupo3,((.*?),(.*?),(.*?),(.*?)), ((.*?),(.*?),(.*?),(.*?)), ((.*?),(.*?),(.*?),(.*?)), ((.*?),(.*?),(.*?),(.*?)), ((.*?),(.*?),(.*?),(.*?)),/', $str, $matches); 

$groups = []; 
unset($matches[0]); 
$matches = array_values($matches); 
$count = count($matches); 
$j=0; 
for($i=1;$i<$count;$i++) 
{ 
    if($i%5 == 0) 
    { 
     $j++; 
     continue; 
    } 
    $groups[$j][] = $matches[$i]; 

} 

var_dump($groups); 

輸出會是這樣的:

array (size=5) 
    0 => 
    array (size=4) 
     0 => string ' tiago3A' (length=8) 
     1 => string ' bola3A' (length=7) 
     2 => string ' mola3A' (length=7) 
     3 => string ' tijolo3A' (length=9) 
    1 => 
    array (size=4) 
     0 => string 'pedro3B' (length=7) 
     1 => string ' bola3B' (length=7) 
     2 => string ' mola3B' (length=7) 
     3 => string ' tijolo3B' (length=9) 
    2 => 
    array (size=4) 
     0 => string 'raimundo3C' (length=10) 
     1 => string ' bola3C' (length=7) 
     2 => string ' mola3C' (length=7) 
     3 => string ' tijolo3C' (length=9) 
    3 => 
    array (size=4) 
     0 => string 'joao3D' (length=6) 
     1 => string ' bola3D' (length=7) 
     2 => string ' mola3D' (length=7) 
     3 => string ' tijolo3D' (length=9) 
    4 => 
    array (size=4) 
     0 => string 'felipe3E' (length=8) 
     1 => string ' bola3E' (length=7) 
     2 => string ' mola3E' (length=7) 
     3 => string 'tijolo3E' (length=0) 
+0

看看我發佈的內容。我不想要每個段落的四個最初元素。我想將這四個要素(限於第3段)分成5個小組。 –

+0

[link] https://regex101.com/r/Imoozr/3 [/ link] –

+0

@ mega6382。基本上說。但是,如何告訴php,括號中的第一個元素與第二個元素((?P 。*?),..)((?P )...)是同一個組? –

0

請原諒這個答案的遲到。如果這個頁面沒有被擱置,這是一個乾淨/直接的解決方案的綜合答案。這是一個完善的解決方案,因爲我不知道如何生成/訪問輸入數據。

輸入:

$text='grupo1, tiago1A, bola1A, mola1A, tijolo1A, pedro1B, bola1B, mola1B, tijolo1B, raimundo1C, bola1C, mola1C, tijolo1C, joao1D, bola1D, mola1D, tijolo1D, felipe1E, bola1E, mola1E, tijolo1E, 

grupo2, tiago2A, bola2A, mola2A, tijolo2A, pedro2B, bola2B, mola2B, tijolo2B, raimundo2C, bola2C, mola2C, tijolo2C, joao2D, bola2D, mola2D, tijolo2D, felipe2E, bola2E, mola2E, tijolo2E, 

grupo3, tiago3A, bola3A, mola3A, tijolo3A, pedro3B, bola3B, mola3B, tijolo3B, raimundo3C, bola3C, mola3C, tijolo3C, joao3D, bola3D, mola3D, tijolo3D, felipe3E, bola3E, mola3E, tijolo3E, 

grupo4, tiago4A, bola4A, mola4A, tijolo4A, pedro4B, bola4B, mola4B, tijolo4B, raimundo4C, bola4C, mola4C, tijolo4C, joao4D, bola4D, mola4D, tijolo4D, felipe4E, bola4E, mola4E, tijolo4E, 

grupo5, tiago5A, bola5A, mola5A, tijolo5A, pedro5B, bola5B, mola5B, tijolo5B, raimundo5C, bola5C, mola5C, tijolo5C, joao5D, bola5D, mola5D, tijolo5D, felipe5E, bola5E, mola5E, tijolo5E,'; 

的方法:(PHP Demo

var_export(preg_match('/^grupo3, \K.*(?=,)/m',$text,$out)?array_chunk(explode(', ',$out[0]),4):'fail'); 

使用preg_match()以提取單個線,然後使用explode()拆分的 「逗號空間」 字符串,然後使用array_chunk()存儲在一個包含4個元素的5個子陣列中。

該模式的目標行號爲grupo3,,然後使用\K重新啓動完整匹配,然後貪婪地匹配每個非換行符並在該行的最後一個逗號之前停止。積極lookahead (?=,)不存儲完整字符串匹配中的最終逗號。

Pattern Demo

我的方法不保留任何開頭和結尾的空格,只值本身。

輸出:

array (
    0 => 
    array (
    0 => 'tiago3A', 
    1 => 'bola3A', 
    2 => 'mola3A', 
    3 => 'tijolo3A', 
), 
    1 => 
    array (
    0 => 'pedro3B', 
    1 => 'bola3B', 
    2 => 'mola3B', 
    3 => 'tijolo3B', 
), 
    2 => 
    array (
    0 => 'raimundo3C', 
    1 => 'bola3C', 
    2 => 'mola3C', 
    3 => 'tijolo3C', 
), 
    3 => 
    array (
    0 => 'joao3D', 
    1 => 'bola3D', 
    2 => 'mola3D', 
    3 => 'tijolo3D', 
), 
    4 => 
    array (
    0 => 'felipe3E', 
    1 => 'bola3E', 
    2 => 'mola3E', 
    3 => 'tijolo3E', 
), 
) 

附:如果搜索詞($needle)是是動態的,你可以使用這樣的事情來達到相同的結果:(PHP Demo

$needle='grupo3'; 
// if the needle may include any regex-sensitive characters, use preg_quote($needle,'/') at $needle 
var_export(preg_match('/^'.$needle.', \K.*(?=,)/m',$text,$out)?array_chunk(explode(', ',$out[0]),4):'fail'); 

/* or this is equivalent... 
    if(preg_match('/^'.$needle.', \K.*(?=,)/m',$text,$out)){ 
     $singles=explode(', ',$out[0]); 
     $groups=array_chunk($singles,4); 
     var_export($groups); 
    }else{ 
     echo 'fail'; 
    } 
*/ 
+0

@AntonioOliveira對不起,我不得不劫持你的問題內容,​​以免它被鎖定。現在它已經打開,我爲您的問題提供了一些精緻的解決方案。如果您對這些技術有任何疑問,請詢問。 – mickmackusa