0

I want to upgrade a project from one version to another so, that in need to change thousands of lines with same pattern.

Example:

From this

 $this->returnData['status']

To this

$this->{returnData['status']}

By using following regex i found all the matches but unable to replace with braces.

->[a-zA-z]{5,15}\['[a-zA-z]{5,15}'\]

I used following to replace

->\{[a-zA-z]{5,15}\['[a-zA-z]{5,15}'\]\}
Emma
  • 27,428
  • 11
  • 44
  • 69

2 Answers2

0

Try using the following find and replace, in regex mode:

Find:    \$this->([A-Za-z]{5,15}\['[A-Za-z]{5,15}'\])
Replace: $this->{$1}

Demo

The regex matches says to:

\$this->             match "$this->"
(                    then match and capture
    [A-Za-z]{5,15}   5-15 letters
    \[               [
    '[A-Za-z]{5,15}' 5-15 letters in single quotes
    \]               ]
 )                   stop capture group

The replacement is just $this-> followed by the first capture group, but now wrapped in curly braces.

If you want to just match the arrow operator, regardless of what precedes it, then just use this pattern, and keep everything else the same:

->([A-Za-z]{5,15}\['[A-Za-z]{5,15}'\])
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

You could also use negated character classes and use \K to forget the match. In the replacement use $0 to insert the full match surrounded by { and }

Match:

->\K[^[\n]+\['[^\n']+']

That will match

  • -> match ->
  • \K Forget what was matched
  • [^[\n]+ Match not [ or newline 1+ times or exactly [a-zA-Z]{5,15} instead of [^[\n]+
  • \[ Match [
  • '[^\n']+' Match from ', 1+ times not ' or newline and then ' or exactly [a-zA-Z]{5,15} instead of [^\n']+
  • ] Match literally

Regex demo

Replace with:

{$0}

Result:

$this->{returnData['status']}

The exacter version would look like:

->\K[a-zA-Z]{5,15}\['[a-zA-Z]{5,15}']

Regex demo

Note that [a-zA-z] matches more than [a-zA-Z]

The fourth bird
  • 154,723
  • 16
  • 55
  • 70