0

I'm working in the Adobe ExtendScript environment (Javascript) trying to match a section from a string, but nothing I try seems to work. Here's an example of the string:

Q1: Question 1: this is the first question?

- Answer bit
- Answer bit
- Answer bit

Q2: Question 2: this is the second question?

- Answer bit
- Answer bit
- Answer bit

Q3: Question 3: this is the third question?

- Answer bit
- Answer bit
- Answer bit

I want to be able to match an entire question and answer bit, so from 'Qx' until the next instance of Q (or Qx+1). Every RegExp I try either just gets the question line, or returns null.

Any help with a solution MUCH appreciated, I still haven't cracked Reg Exp's yet clearly .

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
kinkersnick
  • 1,301
  • 1
  • 13
  • 19
  • Please post your failed attempts anyway so we can build on those and tell you where you went wrong. In the meantime: http://www.regular-expressions.info/tutorial.html – Martin Ender Jul 27 '13 at 10:20
  • This is what I'm trying: Q.*?[\s\S](?=Q) I can dynamically insert the question numbers into the Reg Exp so it would be Q1.*?[\s\S](?=Q2) for example. But it doesn't work. – kinkersnick Jul 27 '13 at 10:42
  • You only allow for a single character with `[\s\S]`. Repeating it like `[\s\S]*?` should help. – Martin Ender Jul 27 '13 at 10:47

2 Answers2

0

You can get it with this pattern:

/Q\d+: (.+)\n\n([\s\S]+?)(?=\n\nQ\d+:|\n*$)/g

Where the question is in the first capture group and answers in the second. The trick to know is that the dot doesn't match newlines, it's why I replace it by [\s\S] when I need newlines.

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
  • Returns null unfortunately. Have been trying the [\s\S] thing and seems to do nothing, can't find any way of flowing over the line breaks! – kinkersnick Jul 27 '13 at 10:36
0

Do you know about lookaheads. You can use a lookahead at every position to check that the current character does not start a new question. If it doesn't, consume the character and repeat. Also note that . does not match line breaks. [^] on the other hand matches any character:

/^Q\d+:(?:(?!Q\d+:)[^])+/gm

Or if you want to capture the question separately,

/^Q\d+:\s*(.*)((?:(?!Q\d+:)[^])+)/gm

Now the question is in group 1 and the answer is in group 2 (which you can trim afterwards).

Martin Ender
  • 43,427
  • 11
  • 90
  • 130