2

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]*)
Community
  • 1
  • 1
Gibbs
  • 21,904
  • 13
  • 74
  • 138
  • Is requirement to match only capitol letter followed by `$` character ? – guest271314 Mar 15 '16 at 05:46
  • @guest271314 No. Only `Q$: C$: A$:` in this sensitive. Others can be small/capital letter – Gibbs Mar 15 '16 at 05:47
  • Is "Q\$:" part of actual input string ? – guest271314 Mar 15 '16 at 06:01
  • `Q$:` is the pattern to identify question and `A$:` is the pattern to identify answer and `C$:` is the pattern to identify choices. Choices are optional. If it is present it should have four choices comma separated and answer and question part is mandatory. If content has 10 questions, and one of those doesn't have valid choice it is invalid string. – Gibbs Mar 15 '16 at 06:03
  • _"If it is present it should have four choices comma separated and answer and question part is mandatory. If content has 10 questions, and one of those doesn't have valid choice it is invalid string."_ This description does not appear at Question. Question is only `RegExp` portion ? – guest271314 Mar 15 '16 at 06:05
  • 1
    It seems you can use [`/^(Q\$:.*)[\r\n]+(A\$:.*)(?:[\r\n]+(C\$:[^,\n]+(?:,[^,\n]+){3}))?/gm`](https://regex101.com/r/qT6mV8/2), or something similar. Maybe something like https://jsfiddle.net/01xeatxy/. – Wiktor Stribiżew Mar 15 '16 at 08:40
  • If you need to first *validate* the string, you need a different regex to test the input. With something like [`^(?:Q\$:.*[\r\n]+A\$:.*[\r\n]*){10}`](https://regex101.com/r/qT6mV8/3). – Wiktor Stribiżew Mar 15 '16 at 08:46
  • Does it mean a question can follow an answer? Then, my approach won't work. It requires a set order of questions and answers and choices. As for the *skipping invalid and pick only valid* - maybe [`https://regex101.com/r/qT6mV8/6`](https://regex101.com/r/qT6mV8/6) can help. – Wiktor Stribiżew Mar 15 '16 at 09:10
  • @WiktorStribiżew Please see my edit – Gibbs Mar 15 '16 at 09:17
  • 1
    You have no capturing groups from @WiktorStribiżew comment. But I improve that line to show the capturing groups: https://regex101.com/r/nK4aS6/1 You can see the different patterns in the matches section. You can extract that matches with javascript and split the choices and you'll get the desired result – Marcos Pérez Gude Mar 15 '16 at 09:42
  • @pranav-c-balan can you post your answer you posted earlier? – Gibbs Mar 15 '16 at 09:50

0 Answers0