How can the regex pattern be modified to avoid capturing empty strings at the end of the input in PHP?
When using a regex pattern in PHP to match a certain pattern in a string, it may inadvertently capture empty strings at the end of the input if the pattern allows for it. To avoid capturing these empty strings, you can modify the regex pattern by adding a negative lookahead assertion at the end to ensure that the match does not end with an empty string.
$input = "abc,def,ghi,";
$pattern = '/\w+(?:,\w+)*(?!,)/'; // Add negative lookahead assertion to avoid capturing empty strings at the end
preg_match_all($pattern, $input, $matches);
print_r($matches[0]);