If you want the Twitter rules, why not use the library from the ones who know the rules better than anyone else: the Twitter themselves? :-)
In case you use Gradle, you can just add compile 'com.twitter:twitter-text:1.12.1'
to the dependencies in your Gradle file.
Or for Maven, add to pom.xml:
<dependencies>
<dependency>
<groupId>com.twitter</groupId>
<artifactId>twitter-text</artifactId>
<version>1.12.1</version>
</dependency>
</dependencies>
Then in your code you can call the Twitter library like this:
import com.twitter.Extractor;
public class Main {
public static void main(String[] args) {
Extractor extractor = new Extractor();
String text = "extracting hashtags and mentions in #java using @twitter library from @github";
System.out.println("#hashtags:");
for (String hashtag : extractor.extractHashtags(text)) {
System.out.println(hashtag);
}
System.out.println();
System.out.println("@mentions:");
for (String mention : extractor.extractMentionedScreennames(text)) {
System.out.println(mention);
}
}
}