DopeDod DopeDod - 2 months ago 10
Java Question

Java parsing mentions from tweet

i'm trying to parse username mentions from specific tweet using regex, but it always returns IllegalStateException that match is not found but i know that regex is good as it works for others found it on this site.

String input = "@rivest talk in 30 minutes #hype";
String regex = "(?<=^|(?<=[^a-zA-Z0-9-_\\\\.]))@([A-Za-z]+[A-Za-z0-9_]+)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);

Could you help me to find mistake here ? Or should I used different regex


You forgot to call find(), which is one of the matching methods, as documented in the javadoc.

String input = "@rivest talk in 30 minutes #hype";
String regex = "(?<=^|(?<=[^a-zA-Z0-9-_\\\\.]))@([A-Za-z][A-Za-z0-9_]+)";
Matcher matcher = Pattern.compile(regex).matcher(input);
if (matcher.find()) {

I also did a small tweak on the regex, as the + after [A-Za-z] seemed pointless.