10

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]
25
  • 2
    it should work..can you try using ^[^"]*".. Commented Aug 10, 2016 at 5:19
  • have you enabled multiline mode (if there is any)? Commented Aug 10, 2016 at 5:21
  • 1
    Thanks Rock and Jan for prompt reply. I think the regex provided by Rock do match the pattern I willing to replace. But once I click on "Replace All" button in the Notepad++. It seem like doing replace after replace, until all the matching pattern are gone. Commented Aug 10, 2016 at 5:28
  • 2
    Are you sure your regex doesn't work? It behaves as if you left out the question mark Commented Aug 10, 2016 at 5:29
  • 1
    @mri I actually think there's a bug in notepad++. If you replace ^.*?" with X (ie something) it works as expected, but if you replace with blank, it doesn't work. I suspect there's a bug where the match pointer is incorrectly set after replacing with blank. Commented Aug 10, 2016 at 5:45

3 Answers 3

9

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]
Sign up to request clarification or add additional context in comments.

2 Comments

Explanation is correct, but the solution is not, try it against alpha = ["first", ["55"], 28]. It might not be something OP will have in the input, but the point is that you just have to match the whole line to be able to use such a replacement with Replace all.
@WiktorStribiżew I understand, there may be many cases like this, though I've only dealt with the code that OP have supplied. Anyways, thanks for pointing it out, I've updated my answer accordingly.
6

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:

enter image description here

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.