Home > Software engineering >  notepad how to replace all [, ] with (, )?
notepad how to replace all [, ] with (, )?

Time:12-19

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]);
}

CodePudding user response:

You can use

\[(\w (\[(?:[^][]  |(?-1))*])*\s*,\s*\w (\[(?:[^][]  |(?-1))*])*)]

See the enter image description here

CodePudding user response:

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

  • Related