#!/usr/bin/perl use strict; use warnings; while () { $_ =~ s/[(]*(?![)]+)//; $_ =~ s/[)]*(?## $_ =~ s/[(]*(?![)]+)//; # Replace 0 or more instances of '(' if the # lookahead assertion for one or more of ')' # fails. This seems to work correctly $_ =~ s/[)]*(?