Reputation: 1662
Hello I need a regex to get a string "trkfixo" from
SIP/trkfixo-000072b6
I was trying to use explode
but I prefer a regex solution.
$ex = explode("/",$sip);
$ex2 = explode("-",$ex[1]);
echo $ex2[0];
Upvotes: 2
Views: 53
Reputation: 626835
You may use '~/([^-]+)~'
:
$re = '~/([^-]+)~';
$str = "SIP/trkfixo-000072b6";
preg_match($re, $str, $match);
echo $match[1]; // => trkfixo
See the regex demo and a PHP demo
Pattern details:
/
- matches a /
([^-]+)
- Group 1 capturing 1 or more (+
) symbols other than -
(due to the fact that [^-]
is a negated character class that matches any symbols other than all symbols and ranges inside this class).Upvotes: 3