将两种模式集成在一起

时间:2017-03-23 14:34:12

标签: java

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”开头的单词,我使用了一个模式来删除这些单词。只使用其中一种模式本身,但如果我同时使用它们,它们就没有任何影响。

我是否可以将两种模式整合在一起?

1 个答案:

答案 0 :(得分:0)

您可以使用(https|@)\\S*使用|

将正则表达式合并到一个组中

(https|@)\\S*匹配https@字符

\\S*:匹配零个或多个非空格字符

所以请使用Pattern.compile("(https|@)\\S*")