I have an array of cities and I would like it to return true if someone types in all or part of a city name. So "lak" ,"l", and "lakesi" would all return true for "lakeside". It must be spelled correctly, I am not just trying to match some letters in a city name and it must start from the beginning of the word, so "lksd" and "kside" would return false. Can you tell me a regular expression to do this?
example:
$city = <STDIN>;
foreach $mycity (@cities_array)
{
if($city =~ [some regex stuff]/$mycity/)
{
print "Some Output";
}
}
Copyright © 2024 Q2A.ES - All rights reserved.
Answers & Comments
Verified answer
Within a regex, use \A to anchor the match at the beginning of the string. So you want
chomp $city;
if ($mycity =~ m/\A$city/i)