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

后端 未结 5 1161
孤独总比滥情好
孤独总比滥情好 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:22

    You don't need to escape it (it's probably the \b that's throwing it off):

    if (preg_match('/^\w+#(\w+)/', 'abc#def', $matches)) {
        print_r($matches);
    }
    
    /* output of $matches:
    Array
    (
        [0] => abc#def
        [1] => def
    )
    */
    

提交回复
热议问题