Search code examples
arraysregex2dnotepad++

notepad++ how to replace all [, ] with (, )?


how to replace all [,] with (,) in notepad++ with regex? like this:

  abc[1, 2]; => abc(1, 2);
  abc[EDGE, 2]; => abc(EDGE, 2);
  abc[EDGE, INDEX]; => abc(EDGE, INDEX);
  ...
  abc[EDGE, otherArray[1]]; => abc[EDGE, otherArray[1]);
  abc[array[VERT], other[INDEX]]; => abc(array[VERT], other[INDEX]);

Edit:

the , matters (it's 2d array), these should be excluded:

    abc[1]; <--- ignore
    abc[1, 2, 3]; <-- ignore
    abc[index]; <-- ignore
    abc[otherArray[EDGE], 2, 8]; <-- ignore

Thank you.

Edit2:

Hi Substitue, your answer works almost perfect, except this line:

if (test[0])
{
    triangles.push_back(tris[i, V1]); // <-- fail here, it gets 'test[0]' involved
    triangles.push_back(tris[i, V2]);
    triangles.push_back(tris[i, V3]);
}

Solution

  • You can use this regex to capture the content between [].
    \[(.*)\]

    To address the need to only match the above examples, you can use
    \[([^,\n]+,[^,\n]+)\]

    See: https://regex101.com/r/tfY6cs/1

    In your replace field you can then use
    \(\1\) to put (<captured group1>)

    If you need to capture [] recursively you have two options.

    The easy option is to just repeat the search/replace.

    The hard option is to rewrite the regex to be recursive.

    See also: https://stackoverflow.com/a/17392520/19192256