manix
manix

Reputation: 14747

Get name from hashtag using regex

I have this string/content:

@Salome, @Jessi H and @O'Ren were playing at the @Lean's yard with "@Ziggy" the mouse.

Well, I am trying to get all names focuses above. I have used @ symbol to create like a hash to be used in my web. If you note, there are names with spaces between like @Jessi H and characters before and after like @Ziggy. So, I don't my if you suggest me another way to manage the hash in another way to get it works correctly. I was thinking that for user that have white spaces, could write the hash with quotes like @"Jessi H". What do you think? Other examples:

@Lean's => @"Lean"'s  
@Jessi H => @"Jessi H"  
"@Jessi H" => (sorry, I don't know how to parse it)  
@O'Ren => @"O'Ren" 

What I have do? I'm starting using regex in php, but some SO questions have been usefull for me to get started, so, these are my tries using preg_match_all function firstly:

Result of /@(.*?)[,\" ]/:

Array ( [0] => Salome [1] => Jessi [2] => Charlie [3] => Lean's [4] => Ziggy" ) )

Result of /@"(.*?)"/ for names like @"name":

Empty array

Guys, I don't expect that you do it all for me. I think that a pseudo-code or something like this will be helpful to guide me to the right direction.

Upvotes: 3

Views: 202

Answers (2)

Kelvin
Kelvin

Reputation: 5317

Try the following regex: '/@(?:"([^"]+)|([^\b]+?))\b/'

This will return two match groups, the first containing any quoted names (eg @"Jessi H" and @"O'Ren"), and the second containing any unquoted names (eg @Salome, @Leon)

$matches = array();
preg_match_all('/@(?:"([^"]+)|([^\b]+?))\b/', '@Salome, @"Jessi H" and @"O\'Ren" were playing at the @Lean\'s yard with "@Ziggy" the mouse.', $matches);
print_r($matches);

Output:

Array
(
    [0] => Array
        (
            [0] => @Salome
            [1] => @"Jessi H
            [2] => @"O'Ren
            [3] => @Lean
            [4] => @Ziggy
        )

    [1] => Array
        (
            [0] => 
            [1] => Jessi H
            [2] => O'Ren
            [3] => 
            [4] => 
        )

    [2] => Array
        (
            [0] => Salome
            [1] => 
            [2] => 
            [3] => Lean
            [4] => Ziggy
        )

)

Upvotes: 3

Jordan Kaye
Jordan Kaye

Reputation: 2887

Are you setting these requirements or can you choose them? If you can set the requirements, I would suggest using _ instead of spaces, which would allow you to use the regex:

/@(.+) /

If spaces must be allowed and you're going with quotes, then the quotes should probably span the entire name, allowing for this regex:

/@\"(.+)\" /

Upvotes: 1

Related Questions