开发者

Regex to extract keyword from tweet

I'm trying to match 'apple' in tweet data f开发者_如何转开发rom Twitter. I want to be able to match it to hashtags too, so a match for 'apple' would be either: 'apple' or '#apple'.

Edit: An example tweet might be:

"Today I am going to eat an apple"

or

"Today I am going to eat an #apple"

I do NOT want to match:

"Today I am going to eat lots of apples"

I managed to match hashtags using the following \s#([^ ]*), how would I make the hashtag optional?

Eventually I need to create two variations, one for case sensitive and one for case insensitive.


To match apple but not apples insert a word boundary at the end:

#?apple\b


You can make the hash optional by appending a question mark:

\s#?([^ ]*)


As the hashtag is optional, you might also need to precede "apple" with a word boundary:

#?\bapple\b
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜