Search code examples
regexnotepad++

Regular Expression to replace text inside tags in Notepad++


Hivemind!

I am trying to edit Memsource *.mxliff files with Notepad++.
When I create a search task, both source and target lines mix up in a huge list.
But I need to make amendments only for lines inside <target>.../<target> tags.

For example:

1. <target>от 15 до 35 °C</target>
2. <target>Допустимый диапазон температур воздуха от -40 °C до +70 °C {1&gt;1)&lt;1}</target>  

Inside these lines I need to replace all instances of degrees with a non-breaking space version:

Find: (\d) °C
Replace with: $1 $2

What is the most optimal way to do so?
Any hints would be much appreciated. Thanks a lot!


Solution

    • Ctrl+H
    • Find what: (?:<target>|\G)(?:(?!target)[\s\S])*?\K(\d+)\s*(°C)(?=.*</target>)
    • Replace with: $1&nbsp;$2
    • CHECK Match case
    • CHECK Wrap around
    • CHECK Regular expression
    • UNCHECK . matches newline
    • Replace all

    Explanation:

    (?:                         # non capture group
        <target>                # opening tag
      |                         # OR
        \G                      # restart from last match position
    )                           # end group
    (?:(?!target)[\s\S])*?      # 0 or more any character but not "target"
    \K                          # forget all we have seen until this position
    (\d+)                       # group 1, 1 or more digits
    \s*                         # 0 or more spaces
    (°C)                        # group 2
    (?=.*</target>)             # must be followed by </target>
    

    Replacement:

    $1              # content of group 1 (i.e. the temperature)
    &nbsp;          # non breaking space
    $2              # content of group 2 (i.e. °C)
    

    Screen capture (before):

    enter image description here

    Screen capture (after):

    enter image description here