3

I'm trying to write a regex that will identify whether a string has 2 or more consecutive commas. For example:

hello,,457
,,,,,
dog,,,elephant,,,,,

Can anyone help on what a valid regex would be?

msrd0
  • 7,816
  • 9
  • 47
  • 82
MisterIbbs
  • 247
  • 1
  • 7
  • 20
  • 1
    [Tools exist all over the net to help you write regex.](http://regex101.com/) It'd be a shame if you didn't use them. – Sotirios Delimanolis Sep 16 '14 at 17:36
  • 3
    And it doesn't even require use of regex – anubhava Sep 16 '14 at 17:37
  • 1
    @anubhava is right: you can just as easily call `value.indexOf(",,")` and check to see whether the result is a non-negative number. Using regex does seem overkill in this case. See [indexOf(String)](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String)) for the details. – Bobulous Sep 17 '14 at 20:50

3 Answers3

9
String str ="hello,,,457";
Pattern pat = Pattern.compile("[,]{2,}");
Matcher matcher = pat.matcher(str);
if(matcher.find()){
    System.out.println("contains 2 or more commas");
}
Bobulous
  • 12,967
  • 4
  • 37
  • 68
user3487063
  • 3,672
  • 1
  • 17
  • 24
  • 1
    You can also use `Pattern.matches(".*[,]{2}.*", str)`, this will be shorter. Note that the Pattern class sometimes act like the regex starts with '^' and ends with '$' – msrd0 Sep 16 '14 at 18:11
  • @msrd0 if he uses the pattern you mentioned, then `System.out.println("contains 2 or more commas");` turns into wrong. – Avinash Raj Sep 16 '14 at 18:42
  • @AvinashRaj Why should it? – msrd0 Sep 16 '14 at 18:55
5

The below regex would matches the strings which has two or more consecutive commas,

^.*?,,+.*$ 

DEMO

You don't need to include start and the end anchors while using the regex with matches method.

System.out.println("dog,,,elephant,,,,,".matches(".*?,,+.*"));

Output:

true
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • 1
    A greedy match with `.*,,.*` could work right?. This will ensure at least 2 commas (which the op wants) – TheLostMind Sep 16 '14 at 18:00
  • Thank you, this one worked better, I had used matches() to identify if the regex has been found. Thanks for your suggestions everyone – MisterIbbs Sep 16 '14 at 18:08
  • @user3929182 [accept an answer which helped you the most.](http://stackoverflow.com/help/accepted-answer) – Avinash Raj Sep 16 '14 at 18:13
  • The engine will actually run this slower using a lazy quantifier, where `^.*,,.*$` might be quicker. I think there is an optimization caused by `.*` that causes the engine to directly search for constants before it tries anything else. So, possibly it searches for `,,` first. –  Sep 16 '14 at 20:25
-1

Try:

int occurance = StringUtils.countOccurrencesOf("dog,,,elephant,,,,,", ",,");

or

int count = StringUtils.countMatches("dog,,,elephant,,,,,", ",,");

depend which library you use: Check the solution here: Java: How do I count the number of occurrences of a char in a String?

Community
  • 1
  • 1
Nikola Dimitrovski
  • 720
  • 1
  • 10
  • 23
  • the op asked for 2 r more , – Kick Buttowski Sep 16 '14 at 17:47
  • if(count>1) //do something – Nikola Dimitrovski Sep 16 '14 at 18:02
  • 2
    I gave the downvote. These methods will count the number of times the comma appears in the entire string, and will not tell you whether or not they appear consecutively. Please edit your answer to meet this requirement. – Bobulous Sep 16 '14 at 18:05
  • I also think the OP wanted to have a regex, so your answer doesn't attempt to answer the question – msrd0 Sep 16 '14 at 18:13
  • 2
    Downvote removed. And I don't see anything wrong with providing an alternative to a regular expression. This site would be much less useful if everyone gave the same answer to every question. – Bobulous Sep 17 '14 at 20:46