Mark Chesser
Mark Chesser

Reputation: 25

Integrating 2 patterns together

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();

I have a text file that contains words starting with "@" and words starting with "https", I have used a Pattern to remove these words. Using just one of the Patterns work by itself, but if I use both of them together they have no affect.

Any idea of how I can integerate both of the patterns together?

Upvotes: 1

Views: 38

Answers (1)

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

you can use (https|@)\\S* to combine your regex into a group using | or

(https|@)\\S* match either https or @ character

\\S* : match zero or more non-space characters

so use Pattern.compile("(https|@)\\S*")

Upvotes: 1

Related Questions