Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notepad++ Find and Replace with Regex

This is my coding:

alpha = ["first", 55, 28]
beta  = ["second", 89, 09]
gamma = ["third", 99, 40]

And this is my intended outcome:

first", 55, 28]
second", 89, 09]
third", 99, 40]

I've tried replace the regex of ^.*?" with empty. But why I get this instead?:

, 55, 28]
, 89, 09]
, 99, 40]
like image 899
user3431399 Avatar asked Aug 10 '16 05:08

user3431399


1 Answers

Find and replace with ^.*?\["

Input

alpha = ["first", 55, 28]
beta  = ["second", 89, 09]
gamma = ["third", 99, 40]

Result

first", 55, 28]
second", 89, 09]
third", 99, 40]

The problem with ^.*?" is that it's replacing all the content one by one

  1. You're starting with this content

    alpha = ["first", 55, 28]
    beta  = ["second", 89, 09]
    gamma = ["third", 99, 40]
    
  2. After first replace (as expected), the content becomes this,

    first", 55, 28]
    beta  = ["second", 89, 09]
    gamma = ["third", 99, 40]
    
  3. Now, in the next replace, the cursor is still at the start of the line. Notice that there is one more double quote " in this line (just after the text first), thus the RegEx is matched again and so it will again replace everything before the first double quote

    , 55, 28]
    beta  = ["second", 89, 09]
    gamma = ["third", 99, 40]
    
  4. When you continue in this manner, you'll get the output as:

    , 55, 28]
    , 89, 09]
    , 99, 40]
    

And so use ^.*?\[" instead of ^.*?"


EDIT

If there are any chances that you have arrays inside your parent array, then you can use ^.*?\=\s*\[\s*".
This regex will also deal with whitespace character.

Input

alpha = ["first", 55, 28]
beta  =   ["second", 89, 09]
gamma =      ["third", 99, 40]
delta = ["fourth", ["55"], 28]

After replace

first", 55, 28]
second", 89, 09]
third", 99, 40]
fourth", ["55"], 28]
like image 93
Raman Sahasi Avatar answered Oct 21 '22 03:10

Raman Sahasi