PrintWriter sentimentText = new PrintWriter("C:\\Users\\markc\\OneDrive\\Documents\\NetBeansProjects\\TwitterTest\\src\\text\\sentimentText.txt");
Pattern linkPattern = Pattern.compile("https\\S*");
Pattern linkPattern2 = Pattern.compile("@\\S*");
for (int i = 0; i < tweetsArray.size(); i++) {
sentimentText.println(linkPattern.matcher(tweets.get(i).getText()).replaceAll(""));
sentimentText.println(linkPattern2.matcher(tweets.get(i).getText()).replaceAll(""));
}
sentimentText.close();
我有一个文本文件,其中包含以“@”开头的单词和以“https”开头的单词,我使用了一个模式来删除这些单词。只使用其中一种模式本身,但如果我同时使用它们,它们就没有任何影响。
我是否可以将两种模式整合在一起?
答案 0 :(得分:0)
您可以使用(https|@)\\S*
使用|
或
(https|@)\\S*
匹配https
或@
字符
\\S*
:匹配零个或多个非空格字符
所以请使用Pattern.compile("(https|@)\\S*")