A tricky replacement expression

General questions about using TextPad

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

Post Reply
Mikko Vatanen

A tricky replacement expression

Post by Mikko Vatanen »

Hi!

I have a text with headings like this

1 Heading Level1
2.1 Heading Level2
2.1.1 Heading Level3
2 Heading Level1

and I want to get it into HTML tagged version like

<h1>1 Heading Level1</h1>
<h2>2.1 Heading Level2</h2>
<h3>2.1.1 Heading Level3</h3>
<h1>2 Heading Level1</h1>

Can anyone tell if it's possible to do this with one replace command?

I have been doing like this:

First I add tags to Level 3 like

Find: ^\([0-9]+.[0-9]+.[0-9]+ .*\)$
Replace: <h3>\1</h3>

Then Level 2 headings with one .[0-9]+ less etc.

Thanks,
Mikko
Andreas

Re: A tricky replacement expression

Post by Andreas »

As there is no possibility to count in Regex you can't count the number of digit groups which you would need as the header level, so no, this can't be done with one Regex.
Mikko Vatanen

Re: A tricky replacement expression

Post by Mikko Vatanen »

Hi!

Maybe it would be possible with two replace commands, like this:

First you replace all headings

1 Heading 1
1.1 Heading 2
1.1.1 Heading 3
2 Heading 1

Like this
<h1><h2><h3>1 Heading 1</h1></h2></h3>
<h1><h2><h3>1.1 Heading 2</h1></h2></h3>
<h1><h2><h3>1.1.1 Heading 3</h1></h2></h3>
<h1><h2><h3>2 Heading 1</h1></h2></h3>

and then do a replace like

search: \(<h1>\)<h2><h3>\([0-9]* .*\)\(</h1>\)</h2></h3>\|<h1>\(<h2>\)<h3>\([0-9]*[.][0-9]* .*\)</h1>\(</h2>\)</h3>\|<h1><h2>\(<h3>\)\([0-9]*[.][0-9]*[.][0-9]* .*\)</h1></h2>\(</h3>\)

replace: \1\2\3

I tthough that I could use the \| operator. So that in the
first clause the groups are like:
\1 = <h1>
\2 = 1 Heading 1
\3 = </h1>

In the second:
\1 = <h2>
\2 = 1.1 Heading 2
\3 = </h2>

etc.

But my expression seems to fail with the third clause. I get result
like this

<h1>1 Heading 1</h1>
<h2>1.1 Heading 2</h2>
<h2><h3>1.1.1 Heading 3
<h1>2 Heading 1</h1>
Is there a way to group expressions and operators? Like in
a usual programming language I would say

( ) OR ( ) OR ( )
Post Reply