2013-06-22 29 views
7

PHPの正規表現を作成して、複数のセクション/条件を1つの文字列から抽出しようとしています...私が何を話しているかを示しましょう。あなたが見ることができるように複数の一致を文字列から抽出する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フィールドの値。名前のグループに配置する必要がありますキャプチャ
  • Typeフィールドは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); 

EDIT:

注意:あなたが抽出するより多くの情報を持っている場合は、 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;  
} 
関連する問題