RandomRegex.pl 895 Bytes
#!/usr/bin/perl

use strict;
use warnings;

my $file;
my @regexTab;
my $sepRegexFile=":=:";
my $sepPoss="<==>";

if($#ARGV != 0 || !$ARGV[0]){
	print STDERR "BAD USAGE : RandomRegex.pl <regex_file>\n";
	exit(1);
}

open($file, $ARGV[0]) or die("Cannot open : $ARGV[0]\n");
while(<$file>){
	chomp $_;
	if(!/^$/ && !/^(\s+)$/){
		my @splittedLine = split(/$sepRegexFile/, $_);
		if($#splittedLine == 1){
			my @tab = ($splittedLine[0], $splittedLine[1]);
			push(@regexTab, \@tab);
		}
		else{
			die("Bad format $ARGV[0] : $_");
		}
	}
}
close($file);

while(<STDIN>){
	for(my $i = 0; $i <= $#regexTab;$i++){
		my @tmpTab = @{$regexTab[$i]};
		if($tmpTab[1] =~ /$sepPoss/){
			my @poss = split(/$sepPoss/,$tmpTab[1]);
			#choose a possibility with a random process
			my $id = rand @poss;
			$_ =~ s/$tmpTab[0]/$poss[$id]/ge;
		}
		else{
			$_ =~ s/$tmpTab[0]/$tmpTab[1]/ge;
		}
	}
	print $_;	
}