How do I use keywords from an array in an regex to search a files.
I'm trying to look at a text file and see if and where the keywords appear. There are two files keywords.txt
keyword.txt
word1
word2
word3
filestosearchon.txt
a lot of words that go on and one and contain linebreaks and linebreaks (up to 100000 characters)
I would like to find the keyword and the position of the match. This works for one word but I am unable to figure out how to iterate the keywords on the regex.
#!/usr/bin/perl
# open profanity list
open(FILE, "keywords.txt") or die("Unable to open file");
@keywords = <FILE>;
close(FILE);
# open text file
local $/=undef;
open(txt, "filetosearchon.txt") or die("Unable to open file");
$txt = <txt>;
$regex = "keyword";
push @section,[length($`),length($&),$1]
while ($txt =~ m/$regex/g);
foreach $element(@section)
{
print (join(", ",@$element), $regex, "\n");
}
How can I iterate the keywords from the array over this while loop to get the matched keywords and position?
Appreciate anyhelp. Thanks