#!/usr/bin/perl -w use strict; my %fieldDescs=( 'ManagedSystem.Product', 'Product Code', 'ManagedSystem.Status', 'Status', # and many more, in actuality loaded from your database ); my %keywords=( EQ => 'is equal to', OFFLINE => "'OFFLINE'", IF => 'if', VALUE => 'the value of', # And your other keywords ); my $text="*IF *VALUE ManagedSystem.Product *EQ NT *AND *VALUE ManagedSystem.Status *EQ '*OFFLINE'"; # Substitute *FOO for the equivalent from %keywords, if it exists $text=~s/\*(\w+)\b/(exists $keywords{$1})?$keywords{$1}:$1/eg; # Substitute any word for its description from fieldDescs, if the words exists there $text=~s/\b([A-Za-z_\.0-9]+)\b/(exists $fieldDescs{$1})?$fieldDescs{$1}:$1/eg; print "New text is\n\t$text\n";