Reputation: 131
So I am doing a list sort of like twitter where I want members to be able to tweat at each other.
What I want to do is compose a regular expression that will extract all data between the @sign and a comma.
For instance,
@foo, @bar, @foo bar, hello world
Currently I have the following expression.
/@([a-z0-9_]+)/i
However, that will stop at the space so instead of registering "@foo bar" as on member it will recognize it at just @foo and ignore the bar portion.
Can somebody help me alter that query so that usernames are allowed to have spaces.
Upvotes: 2
Views: 1078
Reputation: 47640
/@(.*?),/
?
makes *
non-greedy, thats why it's stop after first comma. You should understand that it won't match
@foo
without commas
Upvotes: 0
Reputation: 6082
If you truly seek to match all the data between the @ sign and the comma, you can use
/@(.+),/i
But what I think you need is...
/@([\w +]+),/i
...which matches all word characters (letters, numbers and underscores!) and spaces between the @ and the comma signs. View the demo.
Upvotes: 1
Reputation: 54050
try preg_match_all
<?php
$subject = "@foo, @bar, @foo bar, hello world";
$pattern = '/@(.*),.*/';
$matches = preg_match($pattern, $subject, PREG_SET_ORDER);
print_r($matches);
?>
Upvotes: -2
Reputation: 34855
I think this is what you need:
/@([a-z0-9_\s]+)/i
\s
is the symbol for space.
Upvotes: 0