#!/usr/bin/perl -w use strict; use warnings; my $str = "this.is.example.text"; print "Before: $str\n"; $str =~ s/^(\w+\.\w+\.)(\w+\.\w+)$/[$1] [$2]/; print "After : $str\n"; __END__ Before: this.is.example.text After : [this.is.] [example.text]