Hi, I am trying to construct a regular expression to search and replace a file. The following is the script.
#!use/bin/perl
use strict;
use warnings;
my $line = $ARGV[0];
my $find = "[^a-zA-Z0-9]+seqfile[^a-zA-Z0-9]+=[^a-zA-Z0-9]+[a-z]+..";
my $replace = "done"; open (FILE, ">>/home/user/Desktop/test") || die "cant open file \n";
my @body = <FILE>;
foreach $line (@body) {
if (my $line =~ s/$find/$replace/g){
print FILE $line;
}
else {
print "did not replace \n\n";
}
}
close(FILE);
print "reached here\n";
exit;
The sample test file I am running to test my program consists of few lines of text. The string I want to replace is present on first line being " tobereplaced = file.aa ". I had to use carot (^) for characters other than alphabets/digits because regex for space "\s" is not accepted in my system. I know the program is executed because it prints 'reached here'. Can anyone suggest
- why is my program not able to search for string using the regex I specify.
- Why does my system not recognize '\s' and give error "Unrecognized escape \s passed through at test"
- And also, can anyone suggest some good source for studying regex.
Thanks