2013-06-22 11 views
7

Sto provando a eseguire un'espressione regolare di php per estrarre più sezioni/condizioni da una stringa ... lascia che ti mostri di cosa sto parlando; Questo è un estratto dal contenuto file totale (i veri contenuti contengono centinaia di questi gruppi):php regex per estrarre più corrispondenze dalla stringa

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 : "" } 

Come si può vedere, i dati del brano ripete due volte. Ho bisogno per la ricerca in tutto il file ed estrarre il seguente:

  • stringa dopo la parola "parte" - che sarebbe "C28" o "C29"
  • stringa dopo che la proprietà "tipo" - che sarebbe "1AB010050093" o "1AB008140029"

Quindi, in sostanza, ho bisogno di ottenere tutti i riferimenti di parte e tipi associati di questo file ... e io non sono sicuro che il modo migliore per andare su facendo questo.

Per favore fatemi sapere se sono necessarie ulteriori informazioni per aiutare ... grazie in anticipo!

+0

C'è un motivo per cui non stai usando un parser Json per questo tipo di dati? –

+1

@Denomales Anche se sembra simile, l'esempio non è dati JSON e non funzionerebbe con 'json_decode' di PHP. –

+0

Abbastanza giusto. Dovevo chiedere –

risposta

11

Descrizione

Questa espressione:

  • cattura il nome del gruppo come ref
  • cattura i valori dei campi type e descr.
  • Il campo Tipo quando catturato dovrebbe essere messo in un gruppo denominato chiamato partnumber
  • I campi possono apparire in qualsiasi ordine nel corpo
  • campo descr è opzionale e deve essere catturato solo se esiste. Il campo descr` (?: ... )?`` brackets around the rende il campo facoltativo

Nota che questa è una singola espressione quindi dovrete per usare l'opzione x per modo che il motore regex ignora lo spazio bianco.

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

enter image description here

PHP Esempio di codice:

Text Input

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" } 

Codice

<?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); 
?> 

Partite

$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

davvero bella risposta! :) – hek2mgl

+0

Grazie mille :) –

+0

@Denomales da dove viene l'immagine di visualizzazione delle espressioni regolari? – tristanbailey

2

Supponendo che ogni gruppo hanno la stessa struttura, è possibile utilizzare questo modello:

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

EDIT:

Avviso: se si dispone di informazioni per estrarre, puoi facilmente trasformare i tuoi dati in json, ad esempio:

$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;  
} 
Problemi correlati