0

That is to say, alice should be matched, but bob shouldn't in the following

Hello @alice and [@bob](...)

I can match the names themselves with the following simple regex: /\@([\w]+)/.

Does anyone know how to make the regex not match bob?

Jonny Barnes
  • 515
  • 1
  • 12
  • 28

1 Answers1

2

Group index 1 contains the characters you want.

Use a negative looahead.

@(?![^\[\]]*])(\w+)

DEMO

OR

Through alteration,

\[.*?\]|@(\w+)

DEMO

OR

Through PCRE verb (*SKIP)(*F)

\[.*?\](*SKIP)(*F)|@(\w+)

DEMO

Community
  • 1
  • 1
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • If I understand the last line, the regex is matching anything inside `[]`s and essentially getting rid of it, then we matching the rest of the string for words starting with `@`. – Jonny Barnes Oct 02 '14 at 14:40