Regular expression to count number of commas in a string

How can I build a regular expression that will match a string of any length containing any characters but which must contain 21 commas?

R count number of commas and string

I have a string: str1 <- This is a string, that I’ve written to ask about a question, or at least tried to. How would I : 1) count the number of commas 2) count the occurences of ‘-ion’ Any sug

PHP – Count number of commas in string

How can I count the number of times a comma appears in a string such as this? A,B,C,D It should return 3

Count number of commas within a string except for commas between double quotes

I have the following function to count the number of commas (or any other character) in a String without counting those inside double quotes. I want to know if there’s a better way to achieve this or

Regular expression to count the number of % or = or : in a string of any length

I’m looking for help in crafting a regular expression that detects a count of 10 (or more) of the characters ‘%’ or ‘=’ or ‘:’ in a string of any length. Thus far, I’ve checked the following sources b

Count number of commas using xpath

I am trying to write an xpath expression to count the number of commas in an element. Is it possible to do that? If yes, then could someone help me with the expression. I need to count the commas from

Regular expression in C# for Number with Decimal Point & Commas

I need to enter amount in a textbox which allows numbers with decimal point and commas. What is the regular expression for this? I used the below txtInitialAmountGuarDetails.ValidationSettings.Regular

regular expression to count number of dots for following format

I am new to regular expressions. Can any one help me on how to count number of dots in below formate and limit to 4 dots using regular expression. a->test1().test2().test1().test2().test1().test2()

Regular expression, replace all commas between double quotes

I have this string: 1001,Fitzsimmons, Des Marteau, Beale and Nunn,109,George,COD,Standard,,109,8/14/1998 8:50:02 What regular expression would I use to replace the commas in the Fitzsimmons,

number with decimal places to allow commas in regular expression validation

I am using a regular expression in javascript as /^/s*?(/d+(/./d{1,2})?|/./d{1,2})/s*$/ This is used to check for numbers upto 2 decimal places, like 200.45. I also need to allow more than one commas

Regular expression to split string and number

I have a string of the form: codename123 Is there a regular expression that can be used with Regex.Split() to split the alphabetic part and the numeric part into a two-element string array?

Answers

/^([^,]*,){21}[^,]*$/

That is:

^     Start of string
(     Start of group
[^,]* Any character except comma, zero or more times
,     A comma
){21} End and repeat the group 21 times
[^,]* Any character except comma, zero or more times again
$     End of string
^(?:[^,]*)(?:,[^,]*){21}$
.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,

Exactly 21 commas:

^([^,]*,){21}[^,]$

At least 21 commas:

^([^,]?,){21}.*$

if exactly 21:

/^[^,]*(,[^,]*){21}$/

if at least 21:

/(,[^,]*){21}/

However, I would suggest don’t use regex for such simple task. Because it’s slow.

What language? There’s probably a simpler method.

For example…

In CFML, you can just see if ListLen(MyString) is 22

In Java, you can compare MyString.split(‘,’) to 22

etc…

Might be faster and more understandable to iterate through the string, count the number of commas found and then compare it to 21.

If you’re using a regex variety that supports the Possessive quantifier (e.g. Java), you can do:

^(?:[^,]*+,){21}[^,]*+$

The Possessive quantifier can be better performance than a Greedy quantifier.

Explanation:

(?x)    # enables comments, so this whole block can be used in a regex.
^       # start of string
(?:     # start non-capturing group
[^,]*+  # as many non-commas as possible, but none required
,       # a comma
)       # end non-capturing group
{21}    # 21 of previous entity (i.e. the group)
[^,]*+  # as many non-commas as possible, but none required
$       # end of string
var valid = ((" " + input + " ").split(",").length == 22);

or…

var valid = 21 == (function(input){
    var ret = 0;
    for (var i=0; i<input.length; i++)
        if (input.substr(i,1) == ",")
            ret++;
    return ret
})();

Will perform better than…

var valid = (/^([^,]*,){21}[^,]*$/).test(input);