mike_grinin
mike_grinin

Reputation: 187

RegEx for digits, chars and commas

Please, help me to compose a regular expression to match digits, chars (case doesn't matter) and commas, but with first, last or several in a row commas invalid. Valid string examples: "123,АВc,0aB12,3c", "ABc", "567". Invalid string examples: "123,,456789"; ","; ",,"; ",123,456"; "123,456,".

Upvotes: 0

Views: 134

Answers (5)

gamag
gamag

Reputation: 423

 ^([A-Za-z0-9]+,)*[A-Za-z0-9]+$

If can use PCRE compatible regex.

Upvotes: 3

MikkolidisMedius
MikkolidisMedius

Reputation: 4844

Maybe this works:

^[A-Za-z0-9]+(,[A-Za-z0-9]+)*$

Upvotes: 2

Guffa
Guffa

Reputation: 700252

Match some alphanumerics, then optionally groups consisting of a comma followed by some alphanumerics:

^[\dA-Za-z]+(,[\dA-Za-z]+)*$

Upvotes: 1

fero
fero

Reputation: 6183

/^[0-9a-z]+(,[0-9a-z]+)*$/i (not tested)

Upvotes: 1

Boris Brodski
Boris Brodski

Reputation: 8695

[0-9A-Za-z]+(?:,[0-9A-Za-z]+)*

Upvotes: 1

Related Questions