sealabr
sealabr

Reputation: 1662

PHP Regex matches beween Slash and Subtract

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

Answers (2)

Nick Bull
Nick Bull

Reputation: 9866

$match = preg_match('/\/[a-zA-Z]-/', "SIP/trkfixo-000072b6");

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

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

Related Questions