Darren Ferguon - Weblog

Perl - Manipulating text with regular expressions

A brief demo of using regular expressions to process content in with Perl.

Marco Tag

<?PERL_MACRO id="alpha" value="This is a string that'll be manipulate with a regex!"/"></?PERL_MACRO>

Marco Output

PERL FOR UMBRACO: The remote server returned an error: (404) Not Found. requesting http://www.darren-ferguson.com:80/plex/alpha.plex

Perl Source

use strict;
use utf8;
use CGI;
use Date::Format;

my $query = new CGI;
print $query->header(-charset => 'utf-8');
my $val = $query->param('value');
if($val) {
	$val =~ s|\&|and|gsm;		# Replace & with the word and
	$val =~ s|[^a-zA-Z0-9\s]||gsm;	# Remove any non aplha numerics
	$val =~ tr|[a-z]|[A-Z]|;	# Convert to uppercase
	$val =~ s|\s|_|gsm;		# Replace spaces with underscores
	print $val;	
} else {
	print 'no value specified';
}