Tricky Sort RegExp challenge

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
Brian Donnelly

Tricky Sort RegExp challenge

Post by Brian Donnelly »

Hi,

I am trying to convert the following:

10. This is Question Text
a. Choice a text
b. Choice B text
c. Choice C text
AR choice A feedback
BR choice B feedback
CR choice C feedback

Into this.

10. This is Question text
a. Choice a text
AR choice A feedback
b. Choice B text
BR choice B feedback
c. Choice C text
CR choice C feedback

Since there are numerous questions per file, I need to be able to sort only on the lines within the file. Is there a way to autmatically limit what the Sort command sorts? The Macro doesn't seem to acknowledge selected text.

Also, given the difficulty of multiline RE's I haven't been able to come up with one that accomplishes this. Any suggestions? Am I mising something?
Brian Donnelly

Re: Tricky Sort RegExp challenge

Post by Brian Donnelly »

Bleh. Nothing like posting your problem to the board to jog your brain....

First. Replace \n with a control character like ~ to get rid of the multi-line restrictions.

then it is a matter of selecting and _re-ordering_ the strings via RegExp. thus:

REPLACE:
\(~~a.[^~~]*~~\)\(b.[^~~]*~~\)\(c.[^~~]*~~\)\(AR.[^~~]*~~\)\(BR.[^~~]*~~\)\(CR.[^~~]*~~\)
WITH:
\1\4\2\5\3\6

behold the power of RegExp! Any more elegant responses welcomed though;)
Travis Armstrong

Re: Tricky Sort RegExp challenge

Post by Travis Armstrong »

I can't 'Find in File' with \n, would setting up a control character solve this problem and if so how do you do that?
Brian Donnelly

Re: Tricky Sort RegExp challenge

Post by Brian Donnelly »

Yes, Multi-line RE is not very strong in TextPad. But you can usually achieve the same result with a control character. Once everything is in the same line you can create RE's using the controll Character instead of \n.

To create a one line file just replace \n with a character or string _that you know does not occur elsewhere in your file_ (such as ~ or ~~). I would avoid using any characters that RE already uses such as ^*., etc since that tends to make the search strings tricky (or not work at all).

Than you use that Control Character wherever you would want to use \n in your searches.
Post Reply