i have following variable. i only want to print yes if the variable has “imoport/canada/campingplaces/tobermory” not # or anything. What should insert in a regex for this kind of things.
my $textfile = "# imoport/canada/campingplaces/tobermory
imoport/canada/campingplaces/tobermory
#imoport/canada/campingplaces/tobermory";
my $textNeeded= "imoport/canada/campingplaces/tobermory"
THIS IS WHAT i am using
if ($textfile =~ m/$textNeeded/i) {
print "yes working"
}
note:- i am getting data from differnt text files so some text files might just have “#imoport/canada/campingplaces/tobermory”. I want to avoid those
Despite the quite vague problem description, I think I have puzzled out what you mean. You mean you may have lines where the text is commented out with
#, and you want to avoid matching those.This will match any string inside
$textfilewhich has a newline followed by optional whitespace followed by your string. The/moption makes the regex multiline, meaning that^and$match line endings represented by newlines inside a larger string.You may wish to be wary of regex meta characters in your search string. If for example your search string is
foo[bar].txt, those brackets will be interpreted as a character class instead. In which case you would useinstead. The
\Q ... \Ewill make the text inside match only literal characters.