2013-06-22 12 views
7

我想讓一個php正則表達式從一個字符串中提取多個部分/條件...讓我告訴你我在說什麼;這是從總的文件內容(而實際內容包含數百個這樣的分組)的摘錄:php正則表達式從字符串中提取多個匹配

part "C28" 
{ type  : "1AB010050093", 
    %cadtype : "1AB010050094", 
    shapeid : "2_1206", 
    descr  : "4700.0000 pFarad 10.00 % 100.0 - VE5-VS3", 
    insclass : "CP6A,CP6B", 
    gentype : "RECT_032_016_006", 
    machine : "SMT", 
    %package : "080450E", 
    %_item_number: "508", 
    %_Term_Seq : "" } 
part "C29" 
{ type  : "1AB008140029", 
    shapeid : "2_1206", 
    descr  : "150.0000 pFarad 5.00 % 100.0 Volt NP0 CERAMIC CAPACITOR", 
    insclass : "CP6A,CP6B", 
    gentype : "RECT_032_016_006", 
    machine : "SMT", 
    %package : "080450E", 
    %_item_number: "3", 
    %_Term_Seq : "" } 

正如你所看到的,摘錄的數據重複兩次。我需要在整個文件中搜索並提取如下:一個字「部分」後

  • 字符串 - 這將是「C28」或「C29」
  • 字符串「類型」屬性後 - 這將是「1AB010050093」或「1AB008140029」

所以,基本上,我需要得到所有的部分引用和相關類型了此文件的......,我不知道去了解的最佳方式這樣做。

請讓我知道是否需要更多的信息來幫助...提前感謝!

+0

是否有你沒有使用Json解析器來處理這種數據類型的原因? –

+1

@Denomales儘管看起來很相似,但該示例不是JSON數據,並且不適用於PHP的「json_decode」。 –

+0

夠公平的。我不得不問。 –

回答

11

說明

此表達式將:

  • 捕獲的組名稱作爲ref
  • 捕獲typedescr字段的值。
  • 拍攝時,應放入命名組類型字段叫partnumber
  • 的字段可以按任意順序在體內
  • descr領域是可選的,如果存在的話應該只被捕獲出現。該(?: ... )?`` brackets around the descr`場讓現場可選

注意這是一個表達式,所以你在使用x選項,因此正則表達式引擎忽略空格。

^part\s"(?P<ref>[^"]*)"[^{]*{ 
(?:(?=[^}]*\sdescr\s*:\s+"(?P<descr>[^"]*)"))? 
(?=[^}]*\stype\s*:\s+"(?P<type>[^"]*)") 

enter image description here

PHP代碼示例:

輸入文本

part "C28" 
{ type  : "1AB010050093", 
    %cadtype : "1AB010050094", 
    shapeid : "2_1206", 
    descr  : "4700.0000 pFarad 10.00 % 100.0 - VE5-VS3", 
    insclass : "CP6A,CP6B", 
    gentype : "RECT_032_016_006", 
    machine : "SMT", 
    %package : "080450E", 
    %_item_number: "508", 
    %_Term_Seq : "" } 
part "C29" 
{ type  : "1AB008140029", 
    shapeid : "2_1206", 
    descr  : "150.0000 pFarad 5.00 % 100.0 Volt NP0 CERAMIC CAPACITOR", 
    insclass : "CP6A,CP6B", 
    gentype : "RECT_032_016_006", 
    machine : "SMT", 
    %package : "080450E", 
    %_item_number: "3", 
    %_Term_Seq : "" } 
part "C30" 
{ type  : "1AB0081400 30", 
    shapeid : "2_1206 30", 
    insclass : "CP6A,CP6B 30", 
    gentype : "RECT_032_016_006 30", 
    machine : "SMT 30", 
    %package : "080450E 30 ", 
    %_item_number: "3 30 ", 
    %_Term_Seq : "30" } 

代碼

<?php 
$sourcestring="your source string"; 
preg_match_all('/^part\s"(?P<ref>[^"]*)"[^{]*{ 
(?:(?=[^}]*\sdescr\s*:\s+"(?P<descr>[^"]*)"))? 
(?=[^}]*\stype\s*:\s+"(?P<partnumber>[^"]*)")/imsx',$sourcestring,$matches); 
echo "<pre>".print_r($matches,true); 
?> 

匹配

$matches Array: 
(
[ref] => Array 
    (
     [0] => C28 
     [1] => C29 
     [2] => C30 
    ) 

[descr] => Array 
    (
     [0] => 4700.0000 pFarad 10.00 % 100.0 - VE5-VS3 
     [1] => 150.0000 pFarad 5.00 % 100.0 Volt NP0 CERAMIC CAPACITOR 
     [2] => 
    ) 

[partnumber] => Array 
    (
     [0] => 1AB010050093 
     [1] => 1AB008140029 
     [2] => 1AB0081400 30 
    ) 

) 
+1

真的很好的答案! :) – hek2mgl

+0

非常感謝你:) –

+0

@Denomales你從哪裏得到正則表達式可視化圖像? – tristanbailey

2

假設每個組具有相同的結構,你可以用這個模式:

preg_match_all('~([^"]++)"[^{"]++[^"]++"([^"]++)~', $subject, $matches); 
print_r($matches); 

編輯:

注意:如果你有更多的信息提取,你可以很容易地將你的數據轉換成json,例如:

$data = <<<LOD 
part "C28" 
{ type  : "1AB010050093", 
    %cadtype : "1AB010050094", 
    shapeid : "2_1206", 
    descr  : "4700.0000 pFarad 10.00 % 100.0 - VE5-VS3", 
    insclass : "CP6A,CP6B", 
    gentype : "RECT_032_016_006", 
    machine : "SMT", 
    %package : "080450E", 
    %_item_number: "508", 
    %_Term_Seq : "" } 
part "C29" 
{ type  : "1AB008140029", 
    shapeid : "2_1206", 
    descr  : "150.0000 pFarad 5.00 % 100.0 Volt NP0 CERAMIC CAPACITOR", 
    insclass : "CP6A,CP6B", 
    gentype : "RECT_032_016_006", 
    machine : "SMT", 
    %package : "080450E", 
    %_item_number: "3", 
    %_Term_Seq : "" } 
LOD; 
$trans = array("}\n" => '}, ' , 'part' => '' , 
       "\"\n{" => ':{"' , ':'  => '":' , 
       "\",\n" => '","'); 

$data = str_replace(array_keys($trans), $trans, $data); 
$data = preg_replace('~\s*+"\s*+~', '"', $data); 
$json_data =json_decode('{"'.substr($data,1).'}'); 

foreach ($json_data as $key=>$value) { 
    echo '<br/><br/>part: ' . $key . '<br/>type: ' . $value->type;  
} 
相關問題