$re = '/(?:(\w+)@)?(\w+):(?:\/\w+){2}\/((?:\w|\.)*)/m';
$str = '
test23@testbee:/var/bee/
or
testbee:/var/bee/test.html
I need to extract three string here: test23 (if it is available) , testbee and test.html
So, in Perl,
($user, $sys, $file) = ($source =~ /(\\S*?)\\@?(\\S+?):?[^:]*?([^\\/]+)$/);
for 1. this gives, $user = , $sys = test@testbee, $file = test.html for 2. this gives, $user = , $sys = test, $file = test.html
Is there a way, I can just use one expression to get the user \'test\' if it exists and nothing if it doesnt';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php