azzy81
azzy81

Reputation: 2269

regex wrap strings in quotes

I have an array like the example below but much much bigger (4000 lines long):

array(
   "id" => array(
        "a" => "", 
        "b" => "", 
        "c" => Needs Quotes Around Me
), "id" => array(
        "a" => "", 
        "b" => "", 
        "c" => Needs Quotes Around Me
        "d" => Needs Quotes Around Me
)
);

The string values for some reason dont have the quotes ("") around them and the colon seperator. Some of the strings are numbers but can be treated as a string and some have spaces and the @ symbols as some are email addresses but I need to wrap all of them in "STRING HERE",

Im trying to use reg_replace with something like this =>\s([a-zA-Z0-9\@\s])+$ but it doesnt replace the matched string with the string it found? Ive done quite a bit of googling but cant seem to get it right, please tell me where Im going wrong.

What I end up with is:

array(
       "id" => array(
            "a" => "", 
            "b" => "", 
            "c" => "[a-zA-Z0-9\@\s]",
    ), "id" => array(
            "a" => "", 
            "b" => "", 
            "c" => "[a-zA-Z0-9\@\s]",
            "d" => "[a-zA-Z0-9\@\s]",
    )
);

Upvotes: 0

Views: 712

Answers (2)

protist
protist

Reputation: 1220

This perl script works for the example given

perl -pe 's/(?<==> )(?!"|array\()(.*)/"$1",/' EXAMPLEFILE.txt

the following output is produced:

array(
   "id" => array(
        "a" => "", 
        "b" => "", 
        "c" => "Needs Quotes Around Me",
), "id" => array(
        "a" => "", 
        "b" => "", 
        "c" => "Needs Quotes Around Me",
        "d" => "Needs Quotes Around Me",
)
);

Upvotes: 1

azzy81
azzy81

Reputation: 2269

It required placing circular brackets around the regex. As simple as this sounds its only easy when you know how.

Upvotes: 0

Related Questions