Replacing characters between 2 character strings

General questions about using TextPad

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

Post Reply
jlassell
Posts: 4
Joined: Sat Jan 03, 2009 7:19 pm

Replacing characters between 2 character strings

Post by jlassell »

I tried to find this on the forum but was unable. I hope this isn't a repeat question:

I need to 'remove' EOL characters from between 2 tags and the tags may be across multiple lines of text.

example,

Starting text:
----------------
<Expression>ajjb ;aeif j;; =
jot aldi addoao
flda- ee=10200 </Expression>

Needs to look like this:
--------------------------
<Expression>ajjb ;aeif j;; = jot aldi addo aoflda-ee=10200 /Expression>

i.e, all on one line.

I can do the "find" part using <Expression>.*\n.*</Expression>, but
I can't figure out what the 'replacement string' needs to be.

Thanks much,
-Jeff
ak47wong
Posts: 703
Joined: Tue Aug 12, 2003 9:37 am
Location: Sydney, Australia

Post by ak47wong »

Unfortunately you can't match a variable number of EOL characters in TextPad, so you'll have to do a separate replace for text that spans 2 lines, text that spans 3 lines, and so on.

First, enable POSIX regular expression syntax in Configure > Preferences > Editor.

For text spanning 2 lines:

Find what: (<Expression>.*)\n(.*</Expression>)
Replace with: \1_\2 [replace the underscore with a space]

For text spanning 3 lines:

Find what: (<Expression>.*)\n(.*)\n(.*</Expression>)
Replace with: \1_\2_\3 [replace the underscore with a space]

For text spanning 4 lines:

Find what: (<Expression>.*)\n(.*)\n(.*)\n(.*</Expression>)
Replace with: \1_\2_\3_\4 [replace the underscore with a space]

And so on. You can match text spanning up to 9 lines in this way.
jlassell
Posts: 4
Joined: Sat Jan 03, 2009 7:19 pm

Post by jlassell »

Beautiful! Now I understand how the () /1 /2.. work -- was struggling with that.

Follow-up:
Any way to save these 9 search and replaces off to a 'batch' and run them without having to type in every time?
User avatar
kengrubb
Posts: 324
Joined: Thu Dec 11, 2003 5:23 pm
Location: Olympia, WA, USA

Post by kengrubb »

Yes, you can record a Macro.

Macro, Record to start recording your Macro.

Macro, Stop Recording to stop recording your Macro.

You will be prompted to save it, give it a meaningful name, and add a description.
(2[Bb]|[^2].|.[^Bb])

That is the question.
sosimple
Posts: 30
Joined: Sat May 16, 2009 6:54 am

Post by sosimple »

Hi jlassell,

From looking at your starting text:
Starting text:
----------------
<Expression>ajjb ;aeif j;; =
jot aldi addoao
flda- ee=10200 </Expression>
I see a discrepancy with your ending text...

You say it should look like this:
<Expression>ajjb ;aeif j;; = jot aldi addo aoflda-ee=10200 /Expression>
but I assume you meant this:
<Expression>ajjb ;aeif j;; = jot aldi addoao flda- ee=10200 </Expression>
It looks like what you want is to remove the EOL (newline) from the end of all lines that do NOT end with "</Expression>"

There is no way to exactly specify that in TextPad but this is close:

First, select from the menu to use "POSIX regular expression syntax":
Configure -> Preferences -> Editor

[X] Use POSIX regular expression syntax
Then choose Search-Replace ... (<F8> key):
Find what: ([^>]|[^n][>]|[^o]n>|[^i]on>|[^s]ion>|[^s]sion>|[^e]ssion>|[^r]ession>|[^p]ression>|[^x]pression>|[^E]xpression>|[^/]Expression>|[^<]/Expression>)\n
Replace with: \1

[X] Regular expression

[Replace All]
If you need to add a space between the joined lines then:
Find what: ([^>]|[^n][>]|[^o]n>|[^i]on>|[^s]ion>|[^s]sion>|[^e]ssion>|[^r]ession>|[^p]ression>|[^x]pression>|[^E]xpression>|[^/]Expression>|[^<]/Expression>)\n
Replace with: \1_ Replace underscore with a space

[X] Regular expression

[Replace All]
You need to make sure that you select all the "blue" text from the "From what:" line. If it is split onto multiple lines, you need to cut and paste it together. It does NOT contain any spaces.

Here is that line again, in a "code" block:

Code: Select all

([^>]|[^n][>]|[^o]n>|[^i]on>|[^s]ion>|[^s]sion>|[^e]ssion>|[^r]ession>|[^p]ression>|[^x]pression>|[^E]xpression>|[^/]Expression>|[^<]/Expression>)\n
This search-replace will actually remove newlines from the end of any line that is NOT an exact match to any right-hand substring of "</Expression>"

So, lines like these will NOT join with the next line

Code: Select all

ession>
ssion>
ression>
Lines like these WILL join with the next line

Code: Select all

abc ession>
anythingssion>
5 ression>
If this is not acceptable, then you could perform the steps below just one time. If you like, you can record it as a macro. This will combine all sets of 2 or more lines:

First you need find a character that is NOT found anywhere in your file, or at least is not found as the last character of any line.

For this example, I used the "¤" character. You could probably use any "High-order-8-bit-character" like (Ø ¿ « » º ° ¬ § ¤ or £ ¢ €)

First, search for all lines ending in "</Expression>" and add that character ("¤") to the end like: "</Expression>¤"

Select from the menu to use "POSIX regular expression syntax":
Configure -> Preferences -> Editor

[X] Use POSIX regular expression syntax
Then choose Search-Replace ... (<F8> key):
Find what: (</Expression>)$
Replace with: \1¤

[X] Regular expression

[Replace All]
Then, remove the newline from the end of all lines that do NOT end with "¤":
Search-Replace ... (<F8> key):
Find what: ([^¤])\n
Replace with: \1

[X] Regular expression

[Replace All]
If you need to add a space between the joined lines then:
Find what: ([^¤])\n
Replace with: \1_ Replace underscore with a space

[X] Regular expression

[Replace All]
At this point you have all lines joined, (with a space if needed). Lastly, we need to remove the "¤" from the end of all the "</Expression>¤" lines:

Search-Replace ... (<F8> key):
Find what: (</Expression>)[¤]$
Replace with: \1

[X] Regular expression

[Replace All]
I hope this helps...

Kevin
Post Reply