最佳 HashTag 正则表达式 [英] Best HashTag Regex
问题描述
我正在尝试查找字符串中的所有哈希标签.主题标签来自像 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 事件,让我们使用标记#有趣
this is a #awesome event, lets use the tag #fun
我正在使用 .NET 框架 (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?
推荐答案
这取决于您是要匹配其他字符串中的主题标签(Some#Word")还是可能不是主题标签的内容(We're #1").您提供的正则表达式 #w+
将在这两种情况下匹配.如果您将正则表达式稍微修改为 B#ww+
,您可以消除这些情况,并且只匹配单词边界上长度大于 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.
这篇关于最佳 HashTag 正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!