s1 = 'Makeupby Antonia #makeup #makeupartist #makeupdolls #abhcosmetics'
s2 = 'Makeupby Antonia asia #makeup #makeupartist #makeupdolls'
s3 = 'Makeupby Antonia'
s4 = '#makeup #makeupartist #makeupdolls #abhcosmetics'
s5 = 'Makeupby Antonia asia america #makeup #makeupartist'
Regex应该能够匹配s1
和s2
,这仅仅是因为正常的单词数最多为3,并且这些单词有多个hashtag。你知道吗
我可以使用\b(?<![#])[\w]+
选择普通单词
和
我可以使用[#]{1}\w+
选择hashtag
但当我组合这个表达式时,它就起作用了。你知道吗
如何使用这些单独的正则表达式来生成最终的正则表达式,这些正则表达式也可以跟踪计数?你知道吗
可能有很大的优化空间(可能有依赖项/更少的循环),但这里有一个非regex解决方案,如注释中所述:
如果我正确理解了您的问题,并且您可以假设单词总是在标记之前,那么您可以使用
r'^(\w+ ){1,3}#\w+ #\w+'
:这将输出:
理智的解决方案
将文本拆分为单词,并计算其中有多少以哈希符号开头。你知道吗
正则表达式解决方案
我故意不解释正则表达式,因为我不想你用它。你可能会感到困惑,这应该是一个强烈的迹象,表明这是一个糟糕的代码。你知道吗
相关问题 更多 >
编程相关推荐