微博话题正则表达式匹配 ##

 

import java.util.LinkedHashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author XXX
 * Date: 2019/3/20
 * Description:
 */
public class RegexpUtil {

    /**
     * topicTagPattern 匹配话题正则表达式
     */
    private static final Pattern topicTagPattern = Pattern.compile("#([^#]{1,40})#");

    public static Set<String> getTopicList(String content){
        Set<String> topicList = new LinkedHashSet<>();
        Matcher matcher = topicTagPattern.matcher(content);
        while (matcher.find()){
            String topicName = matcher.group(1);
            topicList.add(topicName);
        }
        return topicList;
    }

    public static void main(String[] args){

        String str = "#哈哈a###这是一个#好####哈哈a##哈#啊圣诞节疯狂#奥斯卡级代付款##as的开发#";
        Set<String> topicList = getTopicList(str);
        System.out.println(topicList);

    }
}

输出:

[哈哈a, 这是一个, 哈, 奥斯卡级代付款, as的开发]

新浪微博的话题长度限制好像是40个字符,

另外 类似 #1#2#3#  只有1,3 才算话题 2不算 相当于 一但匹配成功一个话题 ,二个# 符号就会被消耗。

如果需要返回包含重复的话题,getTopicList 方法返回参数改为 List 就好。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值