Search code examples
textexpressionnotepad++swap

How to swap two if and then statements


I have some code to reform (1000 lines) and I want to go from this

if $one=0 and $two=32 then $dist=1
if $one=0 and $two=15 then $dist=2
if $one=0 and $two=19 then $dist=3

To this

if $one=0 and $dist=1 then $two=32
if $one=0 and $dist=2 then $two=15
if $one=0 and $dist=3 then $two=19

In a few words swap both $two and its value with $dist and its value.

Can it happen with regular expressions of notepad+?

I tried:

if ([^ ]+) and ([^]+) then ([^]+)

Solution

    • Ctrl+H
    • Find what: (\$two=\d+)( then )(\$dist=\d+)
    • Replace with: $3$2$1
    • Replace all

    Explanation:

    (\$two=\d+)     : group 1, contains "$two=1 or more digits"
    (\s+then\s+)    : group 2, literally "then" surrounded by spaces
    (\$dist=\d+)    : group 3, contains "$dist=1 or more digits"
    

    \$ must be escaped because it is a special character in regex.

    Replacement:

    $3$2$1   : group 3 group 2 group 1
    

    Result for given example:

    if $one=0 and $dist=1 then $two=32
    if $one=0 and $dist=2 then $two=15
    if $one=0 and $dist=3 then $two=19