(Following-on from a simpler example: deleting PHP array elements that are each on a single line with Dreamweaver and RegEx.)

I have series of associative arrays in a php file that reuse a set of keys. In this example I want delete all the elements with the key “bb”.

The values of the elements are text strings which sometimes span multiple lines.

$variable => array (
 "aa" => "text1",
 "bb" => "text_
          _on two lines2",
 "cc" => "text3",
 "dd" => "text4",
 ),

$variable2 => array (
 "aa" => "text5",
 "bb" => "text6",
 "cc" => "text_
         _on two lines7",
 "dd" => "text8",
 ),

My solution:

Find (\s"bb")(.*\n)*?(\s"cc".*\n)

Replace $3

Explained
Regex grouped (using brackets) into three sections.

  • \s is the character class of White Space (spaces and/or tab characters)
  • 'Key2' is the Key and the double or single quote marks that wrap it
  • .* is for any number of characters (the dot for any character, the asterisk for any number of)
  • \n is for the line break
  • *? “*” as above (“any number of” / “0 or more”) the “?” makes this ungreedy
  • $3 is the backreference for the third group – the only bit we want to keep

Variations

Find
(\s”bb”)(.\n)?(\s”[a-z]{2}”.*\n)
Will find the next element key

Find
(\s”bb”)(.\n)?(\s”dd”.*\n)

Last updated on 5th September 2018