in reply to Pattern Match n00b
Doesn't the ;?? mean match semi-colon 0 or 1 times and be non-greedy?
It does, but it only makes that part of the match non-greedy. The (.+) remains greedy.
But if you make that part non-greedy, then you will be asking to capture as little as possible following the ':', that might or might not be followed by a ';'. Which means it will capture just a single character.
A couple of ways to approach the problem:
Where (?:;|$) requires a semicolon or the end-of-string, (prefering the former) to terminate the capture.
/$some_variable\:([^;]+)/
Either works:
$pet_list = 'dog:boston.terrier;cat:orange.tabby';; $pets=[]; $pet_list =~ /$_\:(.+?)(?:;|$)/ and push @{$pets},$1 for qw[cat dog]; print for @{$pets};; orange.tabby boston.terrier $pets=[]; $pet_list =~ /$_\:([^;]+)/ and push @{$pets},$1 for qw[cat dog]; print for @{$pets};; orange.tabby boston.terrier
|
|---|