regexreplacetextpad

Regular Expression - Not Matching Full Text/Replacing Other Parts


I have a quiz that looks similar to the following:

1: How old am I?
A: 8 days
B: 75
C: 100
D: 99correct
xxxxxxxxxxxx
2: What is your favorite color?
A: blue
B: greencorrect
C: red
D: blue
xxxxxxxxxxxx

In each case, the correct answer is marked by using the word correct at the end of the correct answer.

What I want to do is to take that correct answer and add it after the xxxxxxxxxxxx, so that it reads:

xxxxxxxxxxxx B: Green.

I am currently using the following regex within Textpad in order to find the correct answer

FIND:     ([A-D]:(?s).*correct(?s).*xxxxxxxxxxxx)
REPLACE:  \1xxxxxxxxxxxx

Unfortunately, it is not working as it is replacing the whole quiz with the whole quiz and then adding xxxxxxxxxxx

But the main question, is how do I replace text that is not actually specified as the 'find text'?

Thank you.

PS. Two answers seem to have missed this, so is probably my fault that this is not clarified:

It shouldn't be adding an extra xxxxxxxxxxxx before the answer, but copying the line eg. B: greencorrect after the final xxxxxxxxxxxx for that question.

Here is sample output for the above example:

1: How old am I?
A: 8 days
B: 75
C: 100
D: 99correct
xxxxxxxxxxxx D: 99
2: What is your favorite color?
A: blue
B: greencorrect
C: red
D: blue
xxxxxxxxxxxx B: green

Obviously, the correct answer could be A,B,C or D.


Solution

  • You can use

    (?m)^([A-D]:.*?)correct$((?:\n.*)*?\nx{3,})
    

    See the regex demo. The replacement pattern should be \1\2 \1.

    Details: