Replace specific block of text with an indented block of text

I'm using Atom text editor, although I am happy to switch to any program supporting regex find and replace.

My goal is to:

Search for any line of text containing the word

*group: 

And to replace that with

*page 
    *group: 

Where *group is intended with a tab after the word *page. I then need all the content directly under group to also be "tabbed" over one.

One possibility is to do the following in Atom:

Find: \*group:(.*)

Replace: *page\n\t\t*group:$1

However, this is problematic as it produces blocks like the following:

*page
    *group: GROUP1
    *question: TEXT QUESTIONS
        *shuffle
        ANSWER CHOICE 1
        ANSWER CHOICE 2
        *save: var_1
        *throwaway
    >> GROUP1_answer = "ANSWER CHOICE 1"

But what I need is to have all the content under "group" indented like so

*page
    *group: GROUP1
        *question: TEXT QUESTIONS
            *shuffle
            ANSWER CHOICE 1
            ANSWER CHOICE 2
            *save: var_1
            *throwaway
        >> GROUP1_answer = "ANSWER CHOICE 1"

Any ideas on how to do this?

1 answer

  • answered 2019-05-15 04:33 Hamed Ghasempour

    You can do it using Notepad++ in two steps.

    1- Add *page right before *group

    2- Add a tab before each line which is not started with *page

    Step 1 :

    • In Notepad++ press CTRL + H

    • In field Find what: put this pattern : (\*group:(((?!\*group).)*))

    • In field Replace with: put this pattern: *page\n$1

    • Press Alt + A

    Now you add *page before every *group.

    Result:

    *page
    *group: GROUP1
        *question: TEXT QUESTIONS
            *shuffle
            ANSWER CHOICE 1
            ANSWER CHOICE 2
            *save: var_1
            *throwaway
        >> GROUP1_answer = "ANSWER CHOICE 1"
    *page
    *group: GROUP1
        *question: TEXT QUESTIONS
            *shuffle
            ANSWER CHOICE 1
            ANSWER CHOICE 2
            *save: var_1
            *throwaway
        >> GROUP1_answer = "ANSWER CHOICE 1"
    

    Step 2:

    • In Notepad++ press CTRL + H again

    • In field Find what: put this pattern : (^((?!\*page.*$).*))

    • In field Replace with: put this pattern: \t$1

    • Press Alt + A

    Result:

    *page
        *group: GROUP1
            *question: TEXT QUESTIONS
                *shuffle
                ANSWER CHOICE 1
                ANSWER CHOICE 2
                *save: var_1
                *throwaway
            >> GROUP1_answer = "ANSWER CHOICE 1"
    *page
        *group: GROUP1
            *question: TEXT QUESTIONS
                *shuffle
                ANSWER CHOICE 1
                ANSWER CHOICE 2
                *save: var_1
                *throwaway
            >> GROUP1_answer = "ANSWER CHOICE 1"
    

    By this, A tab will prepend to all lines which are not started with *page.

    Just make sure matches newline option is not checked.