Regex-replace - Insert character N-times based on result's index

143 Views Asked by At

Currently I'm using Notepad++. I'd like to keep this just to text editing programs, but if this function's not in N++ but in something else, I can download it.

Let's say there's a chain of minified HTML that I'm hoping to make legible again. No spacing, everything's on one line.

<div><div><div><div><div>

I've already got a simple regex replace expression to split up the tags.

Replace (<.*?>) with \1\n\t

<div>
    <div>
    <div>
    <div>
    <div>

I'm wondering if, purely through regular expressions, there's a way to add an addtional \t to each line based on the number of results.

<div>
    <div>
        <div>
            <div>
                <div>

I'm aware there are practical issues with the question. I could easily find an HTML formatter, or in 5 minutes write some JS to do it for me, but I'm mainly trying to expand my understanding of the capabilities and limitations of regular expressions.

1

There are 1 best solutions below

1
On

Since you are using Notepad++ , install the XMLTools plugin.

Then select Plugins > XML Tools > Pretty Print (xml only with line breaks)

and you get (foo added for clarity)

<div>
    <div>
        <div>
            <div>
                <div>foo</div>
            </div>
        </div>
    </div>
</div>