【发布时间】:2014-02-07 19:53:44
【问题描述】:
我正在尝试找到一种快速的方法来获取为每个字符串制作的数组:1- 主题标签,2- 用户在推文文本中提及 3- url。我在 csv 文件中有推文文本。
我解决问题的方法需要很长时间的处理时间,我想知道我是否可以稍微优化一下我的代码。我将为每种匹配类型显示我的正则表达式规则,但只是不发布长代码,我将仅显示我如何匹配主题标签。相同的技术适用于 url 和用户提及。
这里是:
public static String hashtagRegex = "^#\\w+|\\s#\\w+";
public static Pattern hashtagPattern = Pattern.compile(hashtagRegex);
public static String urlRegex = "http+://[\\S]+|https+://[\\S]+";
public static Pattern urlPattern = Pattern.compile(urlRegex);
public static String mentionRegex = "^@\\w+|\\s@\\w+";
public static Pattern mentionPattern = Pattern.compile(mentionRegex);
public static String[] getHashtag(String text) {
String hashtags[];
matcher = hashtagPattern.matcher(tweet.getText());
if ( matcher.find() ) {
hashtags = new String[matcher.groupCount()];
for ( int i = 0; matcher.find(); i++ ) {
//Also i'm getting an ArrayIndexOutOfBoundsException
hashtags[i] = matcher.group().replace(" ", "").replace("#", "");
}
}
return hashtags;
}
【问题讨论】: