3

I would like to get all occurrences of

@something

bounded by any nonalphanumeric character or space.

I tried

[^A-Za-z0-9\s]@(\S)[^A-Za-z0-9]

but it keeps including space after word.

I'll be glad for any help, thanks.

Edit: So issue would be clear, I want to get match from

Line start @word1 something @word2,@word3

all '@word1', '@word2', '@word3'

2
  • @\w+ wouldnt do the trick? Commented Jan 23, 2013 at 19:35
  • Thanks! That seems working! Turns out to be really simple :-) Commented Jan 23, 2013 at 19:49

2 Answers 2

1

Is this what you want?

@\w+

Demo

preg_match_all('#(@\w+)#', 'Line start @word1 something @word2,@word3', $matches);
print_r($matches[1]);

Taking from Madbreak comment, to exclude @ preceded by any character, use this instead

 (?<!\w)@\w+(?=\b)

Demo

Sign up to request clarification or add additional context in comments.

2 Comments

@KillerFrca This will match @ preceeded by alphanumeric characters, which your question stated was not acceptable.
@Madbreaks i added look asertion to exclude @ preceded by any alphanumeric character
0

This

preg_match_all('/[^@]*@(\S*)/', 'blabla @something1 blabla @something2 blabla', $matches);
print_r($matches[1]);

prints

Array
(
    [0] => something1
    [1] => something2
)

1 Comment

[^@] will match alphanumeric characters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.