I am trying to extract a string that is between a comma "," and a closing bracket ")"
here is the string below...
(Punta Gorda, FL, U.S.A.)
From this I am looking to extract U.S.A
the regex that I am currently using returns FL, U.S.A.
this is done with the following regex (?<=,).*(?=\))
What is the easiest way to modify the regex in order to achieve my objective?
Use this one instead:
(?<=,\s)[^,]*(?=\))
Note that your regex would return whitespace before FL, U.S.A
. This one does not.
I'd go with this regex:
([^,\s]+)(?=\))
UPD:
$str = '(london, United kingdom)';
preg_match('~([^,]+)(?=\))~', $str, $matches);
var_dump($matches);
If you don´t want to use Regex, this would work too:
$string = "(Punta Gorda, FL, U.S.A.)";
$last_occurence = strrpos($string, ",");
$country = substr($string, $last_occurence + 1, strlen($string) - $last_occurence - 2);