netbeans replace text with regex

Viewed 52

I need to upgrade my software from php 5.2.6 to 7.00

I have to update some outdated code like building arrays.

Array keys must be declared in double quotes.

I want to use netbeans to search and replace occurrences using RegExr.

Substitution does not have to be performed if the key is named with a variable or contains a number.

I was able to find a nearly working regex expression.

The problem is that the first replaced letter is removed.

I ask for your help and enclose the result I would like.

Thank you

PROBLEM From

$array = array();
$array[penne] = 1;   <- my actual code (deprecated)
$array[$name] = 2;
$array[0] = 3;
$array["gomme"] = 4;

To

$array = array();
$array["penne"] = 1;   <- correction I want
$array[$name] = 2;
$array[0] = 3;
$array["gomme"] = 4;

REPLACE WITH NETBEANS REGEXR

enter image description here RESULT

$array = array();
$array["enne"] = 1;   <- NO!!!    WHY ????
$array[$name] = 2;
$array[0] = 3;
$array["gomme"] = 4;
1 Answers

You can use

\[([^\"$0-9][^][]*)]

See the regex demo.

Details:

  • \[ - a [ char
  • ([^"$0-9][^][]*) - Group 1 (\1): any char other than a ", $ and a digit and then any zero or more chars other than [ and ] chars
  • \] - a ] char.
Related