简体   繁体   中英

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]

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]

Use this regex:

^[^"]*"(.*)$

The quantity in parenthesis is called a capture group , because its contents will be captured for each line in your file for which the replace operation is run. You want to retain this captured material, so use a replacement of \\1 in the dialog box.

Here is a screen capture of how to use it:

在此输入图像描述

you could try this .*=\\s?\\[ . of course all setting as @TimBiegeleisen showed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM