Search code examples
regexnotepad++wikitext

Match all lines where the text is not surrounded by a specific set of characters


I'm trying to do a find-replace (in Notepad++, but the specific tool may not matter) for certain entries in a document. The document has entries that look something like this:

==Some header==
* {{Tselect|Select an option}}
** This is an option.
*** '''Someone:''' The option will have some kind of response.
*** '''Someone else:''' Possibly even multiple lines of response.
** This is a different option.
*** {{Tact|This one has a different type of response, surrounded by brackets}}

In the above example, I only want to match ** This is an option. and ** This is a different option., but none of the other lines.

Additionally, once I've captured that, I want to wrap the text (not the * characters) with the following: {{Topt|The text}}.

I would keep the same number of asterisks, and put the text following the asterisks inside of the {{Topt| and }}.

For example, I would turn ** This is an option. into ** {{Topt|This is an option.}}.

I've been fiddling with the regex in a regex tester, but it's matching either nothing or almost everything. I know it will need to start with ^(\*+) so that it only matches start of lines that have an unlimited number of asterisks followed by a space and so that I can capture the exact number of * characters, and it will need to end with $ so that it's confined to a single line.

What I'm struggling with is how I can exclude anything where the text either starts with something surrounded by three ' characters, and also anything surrounded by the {{ and }} characters.

In case it's relevant, this is wiki-code syntax, and it's bulleted items that are slightly more indented at times.


Solution

    • Ctrl+H
    • Find what: ^\*+ \K(?!{{|''').+$
    • Replace with: {{Topt|$0}}
    • TICK Wrap around
    • SELECT Regular expression
    • UNTICK . matches newline
    • Replace all

    Explanation:

    ^           # beginning of line
    \*+         # 1 or more asterisks followed by a space
    \K          # forget all we have seen until this position
    (?!{{|''')  # negative lookahead, make sure the text doesn't begin with {{ or '''
    .+          # 1 or more any character but newline
    $           # end of line
    

    Replacement:

    {{Topt|     # literally
    $0          # the whole match (i.e. the text to be copied
    }}          # literally
    

    Screenshot (before):

    enter image description here

    Screenshot (after):

    enter image description here