Sunday 15 June 2014

php regex to extract multiple matches from string -


I am trying to create a php regex to remove several sections / conditions from a string ... let me know Tell me I'm talking; This is a fraction of the total file content (the actual contents contain hundreds of these groups):

  part "c28" {type: "1AB010050093",% cadet type: "1AB010050094", shape: "2_1206", descr: "4700.0000 pFarad 10.00% 100.0 - VE5-Vs3", insclass: "CP6A, CP6B", gentype: "RECT_032_016_006", machine: "Mrs.",% package: "080450E"% _item_number: "508 ",% _Term_Seq:" "} of" C29 "{type:" 1AB008140029 ", shapeid:" 2_1206 ", descr:" 150.0000 5.00% 100.0 Volt NP0 ceramic capacitor pFarad ", insclass:" CP6A, CP6B ", gentype:" RECT_032_016_006 ", Machine:" SMT ",% Package:" 080450E ",% _item_number:" 3 ",% _Term_Seq:" "}   

As you can see They are, repeating the data in the quote, I need to search the entire file twice and remove the following:

  • String after the word "part" - that would be "C28" or "C29"
  • After the string "Type" property - which will be "1AB010050093" or "1AB008140029"

    Therefore, let me get the reference and related types of all sections from this file is required .. . And I'm sure it's the best way to go about doing this.

    Please tell me that more information is needed for help ... Thanks in advance!

    description

    This expression will be:

    • Capture the group name to << Code>
    • type and descr fields
    • capture Type field to be taken should be put in a group named partnumber
    • which can appear in any order in the body
    • The descr field is optional and should be captured only if it exists. (?: ... )? Make bracket field optional around `` descr` region

      Note that this is a single expression so that you can use the x option So that the regex engine is ignored in white space. <{P>

      Sdescr \ s *: \ s + "(? P & lt; descr & gt; [^"] *) "))? (? = [^}] * \ Stype \ s *: \ s + "(? P [^"] *) ")

      Enter image details here

      PHP code example:

      input text

        portion "C28" {type: "1AB010050093",% cadtype: "1AB010050094", shapeid: "2_1206", descr: "4700.0000 pFarad 10.00% 100.0 - VE5 -Vs3 "Insclass:" CP6A, CP6B "Gentype:" RECT_032_0l6_006 "machine" Mrs. ",% package:" 080450E "% _item_number:" 508 ",% _Term_Seq:" "} of" C29 "{type "LAB008l40029", Shapeid: "2_l206", Descr: "RECT_032_0l6_006" machine:% package "Mrs.": "150.0000 5.00% 100.0 Volt NP0 ceramic capacitor pFarad" insclass: "CP6A, CP6B", gentype "080450E",% _i tem_number: "3",% _Term_Seq: ""} of "C30" {type: "1AB0081400 30", shapeid: "2_1206 30", insclass: "CP6A, CP6B 30", gentype: "RECT_032_016_006 30", machine " Mrs. 30 ",% Package:" 080450E 30 ",% _item_number:" 3 30 ",% _Term_Seq:" 30 "}   

      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 ', $ "mailing > $ matches Array: ([ref] => Array ([0 ] = & Gt; C28 [1] => C29 [2] => C30) [descr] => Array ([0] => 4700.0000 pFarad 10.00% 100.0 - Ve 5-VS 3 [1] => 150.0000 pFarad 5.00% 100.0 Volt NP Ceramic Apparatus [2] => [partnumber] => Hey ([0] => 1AB010050093 [1] = & gt; 1AB008140029 [2] = & gt; 1AB008140030))

No comments:

Post a Comment