use warnings; use strict; my @sentences = ( "This is [color]some text[/color].\n", "This is a [color]test[/color] oops![/color]\n", ); s!\[color\](.*)\[/color\]!\[blue\]$1\[/blue\]!g for @sentences; s!\[/?color\]!!g for @sentences; print for @sentences; #### This is [blue]some text[/blue]. This is a [blue]test oops![/blue]