if ($HTML =~ m{ 245\d{0,2} # MARC code 245 followed by 0-2 indicators .*? # followed by anything, ungreedy \|a\s # followed by a pipe and the subfield (.*?) # followed by the title, # which can be anything, ungreedy (?:\||<) # followed by a pipe and the next subfield # or, if no subfield, an opening HTML tag bracket }xmgs) { $title = MARC::Field->new('245','','', 'a' => "$1"); } else { $title = MARC::Field->new('245','','', 'a' => "field does not exist"); }