怎么样:
$str = 'hey @johnDoe check out this event, be sure to bring @janeDoe:,@johnnyappleSeed?, @johnCitizen] , and @fredNerk';
preg_match_all('/@(.*?)(?:[?, \]: ]|$)/', $str, $m);
print_r($m);
输出:
Array
(
[0] => Array
(
[0] => @johnDoe
[1] => @janeDoe:
[2] => @johnnyappleSeed?
[3] => @johnCitizen]
[4] => @fredNerk
)
[1] => Array
(
[0] => johnDoe
[1] => janeDoe
[2] => johnnyappleSeed
[3] => johnCitizen
[4] => fredNerk
)
)
解释:
The regular expression:
(?-imsx:@(.*?)(?:[?, \]: ]|$))
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
@ '@'
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
[?, \]: ] any character of: '?', ',', ' ', '\]',
':', ' '
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
$ before an optional \n, and the end of
the string
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------