#!/usr/bin/env perl -l use strict; use warnings; my @test_strings = ( 'a { b } c ( d ) e', 'a { b } c ( d ) e {{F}} ((G))' ); my %replacement_for = ( '{' => ' lbracket ', ' { ' => ' lbracket ', '}' => ' rbracket ', ' } ' => ' rbracket ', ); for (@test_strings) { print "Initial string: '$_'"; replace(); print "Replaced string: '$_'"; } sub replace { s/( { | } |{(?!{)|(?