I have been using following Regex to parse @username from posts in my application.
开发者_JAVA技巧'/(^|\s)#(\w*[a-zA-Z_]+\w*)/
Can somebody explain me the purpose of (^|\s)
. What if I omit that part?
(^|\s)
either matches the beginning of a string (^
) or a space character (\s
). This is in order to prevent hallo#world
from matching as a mention.
An alternative to that is using \b
(a word boundary). It has slightly different semantics, but it should work in this case.
(^|\s)
is either the start of the line or string (^
) or (|
) a white space character (\s
)
精彩评论