The task is to match, if string contains commas, with exception of commas, which separate 2 digits.
For example, this address
132 My Street Bigtown BG2A 4,5 England
must not be matched, because it contains comma, but this comma separate 4 and 5 – digits.
And this one:
132, My Street, Bigtown, BG2A 4,5, England
must be matched, because it contains comma, which not only separate digits, but also digit and char (“132, My Street…”), digit and char (“…5, England”).
Simply put, other examples:
Expressions:
s,d,1,1
1,s,1,1
s,s,f,f
1,s,1,2
s,1,s,1
s,1,1,s
must be matched
And:
111,111
1,2,3,4
mm 1,1
sfvdfv1,2
must not be matched
My decision is:
^.*[1-9]((,|, )[a-zA-Z]).*$|^.*[a-zA-Z]((,|, )[1-9]).*$|^.*[a-zA-Z]((,|, )[a-zA-Z]).*$
This regex consists of 3 parts, conjunct by logical “Or” (|):
^.*[1-9]((,|, )[a-zA-Z]).*$
match comma between any digit and any char
^.*[a-zA-Z]((,|, )[1-9]).*$
match comma between any char and any digit
^.*[a-zA-Z]((,|, )[a-zA-Z]).*$
match comma between any char and any other char
This works. But expression seems to be bulky, hard to read and logical redundant generally.
I also looked towards something like:
^.*(,|, ).*$&^.*[^[1-9]((,|, )[1-9])].*$
I expected, ^.(,|, ).$ match comma between any tokens and ^.[^[1-9]((,|, )[1-9])].$ not match commas between digits. So it must any commas except commas, which separate 2 digits.
But this one does not work.
6