最佳的hashtag正则表达式正则表达式、hashtag

2023-09-02 11:47:52 作者:小乖买糖吃

我试图找到所有的哈希标签的字符串。该井号标签从Twitter之类的流时,他们可以在任何地方的文本,如:

I'm trying to find all the hash tags in a string. The hashtags are from a stream like twitter, they could be anywhere in the text like:

这是一个#awesome事件,让使用   标签#fun

this is a #awesome event, lets use the tag #fun

我在使用.NET Framework(C#),我想这会是一个合适的正则表达式使用方法:

I'm using the .NET framework (c#), I was thinking this would be a suitable regex pattern to use:

# w +

这是为了这个目的最好的正则表达式?

Is this the best regex for this purpose?

推荐答案

这取决于你是否想匹配内的其他字符串(有些#字)或东西,可能不是井号标签(#标签我们# 1)。你给了# w +的正则表达式将在这两种情况下匹配。如果你稍微修改您的正则表达式 B# W W + ,可以消除这种情况下,只有匹配长度大于1的井号标签上的字边界。

It depends on whether you want to match hashtags inside other strings ("Some#Word") or things that probably aren't hashtags ("We're #1"). The regex you gave #w+ will match in both these cases. If you slightly modify your regex to b#ww+, you can eliminate these cases and only match hashtags of length greater than 1 on word boundaries.