KillerFrca
KillerFrca

Reputation: 135

PHP regex alphanumeric bounded by nonalpha numeric

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'

Upvotes: 3

Views: 97

Answers (2)

slier
slier

Reputation: 6750

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

Upvotes: 1

Audio
Audio

Reputation: 476

This

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

prints

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

Upvotes: 0

Related Questions