my @state; while (<>) { push @state, 'input' if //; push @state, 'ordered' if //; push @state, 'bulleted' if //; pop @state if // || /<\/input>/; my $state = $state[$#state]; s///g if $state eq 'ordered'; s///g if $state eq 'bulleted'; print; }