Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>Description</h1> <p>This expression will:</p> <ul> <li>capture the group name as <code>ref</code></li> <li>capture the values of the <code>type</code> and <code>descr</code> fields. </li> <li>The Type field when captured should be put into a named group called <code>partnumber</code></li> <li>The fields can appear in any order in the body</li> <li>the <code>descr</code> field is optional and should only be captured if it exists. The <code>(?:</code>...<code>)?`` brackets around the</code>descr` field makes the field optional</li> </ul> <p>Note this is a single expression so you'll in to use the <code>x</code> option to so the regex engine ignore white space.</p> <pre><code>^part\s"(?P&lt;ref&gt;[^"]*)"[^{]*{ (?:(?=[^}]*\sdescr\s*:\s+"(?P&lt;descr&gt;[^"]*)"))? (?=[^}]*\stype\s*:\s+"(?P&lt;type&gt;[^"]*)") </code></pre> <p><img src="https://i.stack.imgur.com/aNUxe.png" alt="enter image description here"></p> <h1>PHP Code Example:</h1> <p><strong>Input Text</strong></p> <pre><code>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" } </code></pre> <p><strong>Code</strong></p> <pre><code>&lt;?php $sourcestring="your source string"; preg_match_all('/^part\s"(?P&lt;ref&gt;[^"]*)"[^{]*{ (?:(?=[^}]*\sdescr\s*:\s+"(?P&lt;descr&gt;[^"]*)"))? (?=[^}]*\stype\s*:\s+"(?P&lt;partnumber&gt;[^"]*)")/imsx',$sourcestring,$matches); echo "&lt;pre&gt;".print_r($matches,true); ?&gt; </code></pre> <p><strong>Matches</strong></p> <pre><code>$matches Array: ( [ref] =&gt; Array ( [0] =&gt; C28 [1] =&gt; C29 [2] =&gt; C30 ) [descr] =&gt; Array ( [0] =&gt; 4700.0000 pFarad 10.00 % 100.0 - VE5-VS3 [1] =&gt; 150.0000 pFarad 5.00 % 100.0 Volt NP0 CERAMIC CAPACITOR [2] =&gt; ) [partnumber] =&gt; Array ( [0] =&gt; 1AB010050093 [1] =&gt; 1AB008140029 [2] =&gt; 1AB0081400 30 ) ) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload