Home » Php » regex – Ruby script to match and replace double quotes to single quotes on PHP files

regex – Ruby script to match and replace double quotes to single quotes on PHP files

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m trying to code a ruby script to replace double quotes to single quotes in PHP files when there’s no parsing happening, need some help with the regex seeing that I’m still trying to learn it. Got something like this for string interpolation matching: \"([^\$].*?)\" but it still match patterns with $ in it.

Example

How it’s supposed to work

Before running the script:

// Normal string
$first = "some value";
// With var concatenation:
$second = "first var value is " . $first . "!";
// With var interpolation:
$third = "first var value is $first!";
// Arrays
$arr["fourth"] = $first;

Example after running the script:

// Normal string
$first = 'some value';
// With var concatenation:
$second = 'first var value is ' . $first . '!';
// With var interpolation:
$third = "first var value is $first!";
// Arrays
$arr['fourth'] = $first;

Any idea of regex I could use to match these scenarios?

How to&Answers:

You don’t need this “.*” because it will match all char.
try this:

"([^$]+)"