Reputation: 663
I have a string: Glass with digitizer - black (iPhone 6)
Now, I want to extract two substrings: Glass with digitizer - black
and iPhone 6
.
I can make it this with:
$string = 'Glass with digitizer - black (iPhone 6)';
$string = explode(' (', $string);
$first = $string[0];
$second = substr($string[1], 0, -1);
However, I want to achieve the same with a regex because the format is constant: first
(second)
.
Upvotes: 0
Views: 37
Reputation: 17831
/^(.+?)\s*\(([^)]+)\)$/
Should work well enough
https://regex101.com/r/wD9iU6/2
Upvotes: 1