#/usr/bin/perl use strict; use warnings; my $string_original =<< "END"; ("Immunologic and Biological Factors"[MESH] OR "Immunosuppressive Agents"[MESH] OR "Transplantation Immunology"[MESH] OR "Allergy and Immunology"[MESH] OR "Graft vs Host Disease"[MESH]) NOT ("Foo"[MESH] OR "Bar"[MESH]) AND ("Kidney Transplantation"[MESH] OR "Liver Transplantation"[MESH] OR "Heart Transplantation"[MESH]) NOT ("My Term"[MESH] OR "Blah"[MESH]) NOT "foobar"[MESH] END # original { my $string = $string_original; $string =~ s/ NOT ".*?"\[MESH\] ?//g; $string =~ s/ NOT \(.*?\) ?//g; print $string, "\n"; } # Corion's { my $string = $string_original; $string =~ s! NOT (?:".*?"\[MESH\]|\(.*?\)) ?!!g; print $string, "\n"; } # fixed { my $string = $string_original; $string =~ s! ?NOT (?:".*?"\[MESH\]|\(.*?\)) ?!!g; print $string, "\n"; }