What Is the Regular Expression to Validate a Comma Delimited List But Ending With '&' and a Word

Regex for Comma delimited list

I suggest you to do in the following way:

(\d+)(,\s*\d+)*

which would work for a list containing 1 or more elements.

How to write regex to verify a comma delimited list of values

Try this:

^[-\w\s]+(?:,[-\w\s]*)*$

Using ^ and $ ensures that we validate the entire value, and don't just find a match somewhere within.

The first character class, [-\w\s]+ allows one or more alphanumeric, whitespace, or dash characters. The dash should go first in the class brackets.

The second group allows zero or more repetitions with separating commas. It is wrapped in non-capturing parentheses, a small performance optimization: (?: … )*

Notes:

  • This expression allows empty entries, such as A,B,,D. If you don't want to allow this, change the second-to-last * to a +.
  • The \w shorthand allows underscores. To prevent this, replace them with A-Za-z0-9.

Regular expression for a list of items separated by comma or by comma and a space

What you're looking for is deceptively easy:

[^,]+ 

This will give you every comma-separated token, and will exclude empty tokens (if the user enters "a,,b" you will only get 'a' and 'b'), BUT it will break if they enter "a, ,b".

If you want to strip the spaces from either side properly (and exclude whitespace only elements), then it gets a tiny bit more complicated:

[^,\s][^\,]*[^,\s]*

However, as has been mentioned in some of the comments, why do you need a regex where a simple split and trim will do the trick?

Validate comma separated list of numbers with regex

Use the following regex:

^\d+(?:\.\d{0,2})?(?:,\s*\d+(?:\.\d{0,2})?)*,?$

Demo

Rather than giving the typical verbose (and illegible) full breakdown of the pattern I used, I will instead explain a simpler requirement. Let's say you wanted to validate a CSV list of capital letters, e.g.

A,B,C

The pattern you would use is:

^[A-Z](?:,[A-Z])*,?$

Note that the term on the right says to allow zero or more additions of comma followed by another letter. Your requirement also allows a trailing comma, so the above pattern includes that too.

validate comma separated list using regex

I don't know what the separate values should look like, but perhaps this is something that can help you:

$value = '[0-9 A-Z]+';
$match = "~^$value(,$value)*$~i";

When you know what your values should look like you can change $value.



Update

Taken this comment from the question

I was trying to disallow 0-9 but allow a-Z, because, as I said it should be a list of words only, no spaces, just a list of single words.

Just change $value

$value = '[A-Z]+';

Note, that I use the i-modifier in $match, that will include all lowercase letters too.


However, "real" csv allows a little bit more than this, for example you can put quotes around every value. I would recommend that you parse the line and then test every single value

$values = str_getcsv($line);
$valid = true;
foreach ($values as $value) {
$valid = $valid && isValid($value);
}

str_getcsv()

Regex to validate a comma separated list of unique email addresses

As discussed in the comments and chat: we have to use negative lookbehind expression((?<![\w.\-+%])) while checking entire string for duplicate email address.

^(?!.*((?:(?<![\w.\-+%])[\w._%+-]+@[\w.-]+.[a-zA-Z]{2,}\b)).*\b\1\b)(?:[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})(?:,(?:[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}))*$

RegEx match open tags except XHTML self-contained tags