Reputation: 496
I'm very new to regex and i'm struggling with a tiny project which can really help in my project. I want to know how to count groups of (consecutive)sequence of exclamation point. For instance lets consider the following string
String s = "OMG!!!, i love Computers !!!! and this !!! is really good!"
the count here should return 3. I tried the following, but it's nothing like what i want public static int exclamation(String list) throws Exception{
String[] words = (list.split("\\s+"));
Pattern pattern = Pattern.compile("\\*?(!!)*\\b");
int count = 0;
for(String s:words)
{
Matcher matcher = pattern.matcher(s);
if(pattern.matcher(s) != null)
{
System.out.println(s);
}
}
return count;
}
Upvotes: 1
Views: 1784
Reputation: 174796
Your regex won't print any ! mark because of \b
. Your regex expects a word character to be present next to !
, but in real there isn't a word character following !
but a non-word character space.
Pattern pattern = Pattern.compile("\\*?(!{2,})");
or
Pattern pattern = Pattern.compile("!{2,}");
Upvotes: 2