I have to write a regex which matches following strings
Q$:ADD A GOOD QUESTION
A$:ANSWER
C$:choice0, choice1, choice2, choice2
Q$:ADD A GOOD QUESTION
A$:ANSWER
C$:choice0, choice1, choice2, choice3
Here,
One match of regex should have Q$:
and A$:
and C$:
is optional. It can be in any order. It can have any characters. And It can be in any order Q$:, C$:, A$: or Q$: A$: C$:
But it should not be Q$: Q$: C$: A$: A$: C$:
I found that we cannot use recursive pattern in Javascript
I have tried but I am not able to do with regex. I am able to do with string operations
but it is lengthy.
Any idea?
EDIT
I am trying to write regex something like Q$:any number of words and A$: any number of words C$: this is optional but if it is present it should have 3 comma and 4 words
. And repeat this pattern
EDIT
Valid strings
Q$:question
A$:answer
C$:c,c1,c2,c3
Q$: question goes here
A$: Answer goes here
C$: choices, goes , here, ch
But there should not be any space between Q $ :
.
And I assume the orders
Q$: question goes here
A$: Answer goes here
C$: choices, goes , here, ch
Q$: question goes here
C$: choices, goes , here, ch
A$: Answer goes here
should also be valid. Question must come first and choice/answer goes.
And
Q$: question goes here
A$: Answer goes here
This also valid string. Other valid strings are
Q$: question goes hereA$: Answer goes here
Q$: question goes here A$: Answer goes here
And even choice also can come in the first line. And Can be space limited.
NOW I don't expect regex way alone. Is there any other ways to do it efficiently? I mean reusable and easily changeable. I have removed regex
tag and added php
tag also.
FROM @Wiktor's answer, I have come up with the following regex. But not up to the level I have expected
(?:Q\$:.*[\S]*[\r\n]*A\$:.*[\r\n]*C\$:.*,.*,.*,.*)|(?:Q\$:.*[\S]*[\r\n]*A\$:.*[\r\n]*)