Looking for a regex
that could work in both Javascript
and Java
.
The whole String should match as long as each capital alphabet follows by a :Y
.
All following examples should match in full.
So Any Capital letter followed by a :Y
. Like A:Y
.
If there is more than 1 pair it is separated by a comma.
S:Y,P:Y,T:Y,A:Y
S:Y,P:Y,T:Y,A:Y,C:Y
S:Y,P:Y,T:Y,A:Y,C:Y,D:Y
A:Y
B:Y
D:Y,F:Y
All the following should never match. Cos 1 or more of them has :N
.
S:Y,P:N,T:Y,A:Y
S:Y,P:Y,T:Y,A:Y,C:N
S:Y,P:Y,T:N,A:Y,C:N,D:Y
A:N
B:N
D:Y,F:N
I don’t want to match in negative, like :N
doesn’t exist.
^(?!.*:N).+$
Looking for a way to match :Y
instead.
Tried the following but this ends up with multiple mini matches which is not what I am looking for.
[SPTA:Y,]+
This could work. But again, this has multiple mini matches.
(?:[A-Z]:Y(?:,|$))+
Could I get some help with this pls.
2
Answers
You could use this regex:
^
: Assert the we’re at the start of the input[A-Z]
: Match a capital English letter:Y
: Math literally ":Y"(?: )*
: repeat the enclosed pattern 0 or more times,
: a literal comma$
: Assert we’re at the end of the input.Just use this, as all are full lines, just anchor them to beggining or end
^
asserts position at start of a line[A-Z]
matches a single character in the range betweenA
andZ
:Y
matches the characters:Y
literally.(,[A-Z]:Y)*
.*
matches the group between zero and unlimited times, as many times as possible.,
matches the character,
[A-Z]
matches a single character in the range betweenA
andZ
.:Y
matches the characters:Y
literally.$
asserts position at the end of a line.This is: groups of one or more
[A-Z]:Y
separated by,
and of arbitrary length.See demo.