How to match a pound (#) symbol in a regex in php (for hashtags)

后端 未结 5 1198
孤独总比滥情好
孤独总比滥情好 2021-01-17 17:02

Very simple, I need to match the # symbol using a regex. I\'m working on a hashtag detector.

I\'ve tried searching in google and in stack overflow. One

5条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-17 17:19

    With the comment on the earlier answer, you want to avoid matching x#x. In that case, your don't need \b but \B:

    \B#(\w\w+)

    (if you really need two-or-more word characters after the #).

    The \B means NON-word-boundary, and since # is not a word character, this matches exactly if the previous character is not a word character.

提交回复
热议问题