I'm using the below code as a test for later when using the Twitter API.
When I fetch tweets from my stream and store them in mySQL, how do I remove the hashtags along with any trailing characters? (basically omitting everything inside the hashtag)
I am able to use replaceAll and pass the "#" and "," values and replace them with "". It works and the output is below the code, but how would I use to replaceAll of the contents of the hashtag including the "not" "#not"?
public class replaceAllTest {
public static void main (String args[]){
String sampleString = "This game was amazing, #not";
System.out.println("Before: " + sampleString);
sampleString = sampleString.replaceAll("#", "");
sampleString = sampleString.replaceAll(",", "");
System.out.println("After: " + sampleString);
}
}
The output from the above code:
Before: This game was amazing, #not
After: This game was amazing not
Expected output from the above code:
Before: This game was amazing, #not
After: This game was amazing
Any help would be appreciated, thanks Z19
You need to use regex
sampleString = sampleString.replaceAll("#[A-Za-z]+","");
If you are expecting lone hashtags not followed by text, use "#[A-Za-z]*"
The comment on your question addresses hashtags that are followed by more than just alpha characters - "#[^\\s]+
where [^\\s]
means anything not whitespace.