Regular expression pattern in java crashes with one specific text

I have the following template for checking for any text:

public static boolean endWithLinkOrHashAt(String commentstr)
{
    String urlPattern = "^[@|#]((?:\\w+\\s?){1,}).*:\\s[^?]?((?:\\w+\\s?){1,})[^?]((?:http|https):\\/\\/\\S+)(\\s[@|#]\\w+){0,}[^?]$";
    Pattern p = Pattern.compile(urlPattern,Pattern.CASE_INSENSITIVE);
    Matcher m = p.matcher(commentstr);
    if (m.find()) {
        System.out.println("yes");
        return true;
    }
    return false;
}

Now, when I try to do this with the following text, the program does nothing, the console starts forever without any result or any error:

endWithLinkOrHashAt("#BREAKING: @Baird resigning in aftermath of controversial win over @pmharper in game of #Trouble (with the pop-o-matic bubble) #cdnpoli");

Something is wrong with my regular expression (but it works with other texts and it seems that it only has a problem with this particular text)

Update:

Here is what I want my template to check:

@ or # + 1 or 2 words + : + 1 words or more + link + nothing or any words that has # or @ at the beginning
+4
source share
1 answer

The problem with your regex seems to be that it caused a catastrophic rollback. The root invokes nested quantifiers.

:

(?i)^[@#](\\S+(?:\\s+\\S+)?)\\s*:\\s*(\\S+(?:\\s+\\S+)*)\\s*(https?://\\S*)((?:\\s+(?=[#@])\\S+)*)\\s*$

, , .

+2

Source: https://habr.com/ru/post/1607279/


All Articles