簡體   English   中英

php regex從字符串中提取多個匹配項

[英]php regex to extract multiple matches from string

我正在嘗試制作一個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”后面的字符串 - 這將是“C28”或“C29”
  • “type”屬性后面的字符串 - 這將是“1AB010050093”或“1AB008140029”

所以,基本上,我需要從這個文件中獲取所有部件引用和相關類型......而且我不確定這樣做的最佳方法。

如果需要更多信息,請告知我們...提前感謝!

描述

這個表達式將:

  • 捕獲組名稱為ref
  • 捕獲typedescr字段的值。
  • 捕獲時的Type字段應放入名為partnumber的命名組中
  • 字段可以按正文中的任何順序出現
  • descr字段是可選的,只有在它存在時才能被捕獲。 descr`字段)?`` brackets around the (?: ... )?`` brackets around the使該字段可選

請注意,這是一個單獨的表達式,因此您將使用x選項,以便正則表達式引擎忽略空格。

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

在此輸入圖像描述

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
    )

)

假設每個組具有相同的結構,您可以使用此模式:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM