So let's say I have a list of strings which sometimes end with a phrase that has been cut off to different lengths. In this example the phrase is "hello".
my @strings =
(
"Test 1 hello",
"Something else",
"Test 2 hell",
"And also he",
"Test 4 hel"
);
This is how I would remove the "hello" fragments right now:
foreach my $string (@strings)
{
if ($string =~ m/(.*?)\s*(h(e(l(lo?)?)?)?)?$/)
{
print "'", $string, "' -> '", $1, "'\n";
}
}
It does work:
'Test 1 hello' -> 'Test 1'
'Something else' -> 'Something else'
'Test 2 hell' -> 'Test 2'
'And also he' -> 'And also'
'Test 4 hel' -> 'Test 4'
However, I find the regular expression to match all the "hello" fragments long, confusing and hard to modify for future use cases.
Is there a shorter way to write something equivalent to (h(e(l(lo?)?)?)?)?$?