RandomRegex.pl
895 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/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 $_;
}