Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to add quotation marks for characters that are inside brackets and separated by a comma?

The idea is to add quotation marks for all strings inside an string list representation.

Tried the next:

input_text <- "[innovation manager, manager director, senior manager]"

scan(text=input_text, what="")

Which returns:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

'[innovation" "manager,"    "manager"     "director,"   "senior"      "manager]'

Expected output could look like this:

'["innovation manager", "manager director", "senior manager"]'

What am I missing?

>Solution :

One method – remove the square brackets with gsub, split at the , (strsplit), extract the list element, insert the double quotes (dQuote), and paste the split elements together

sprintf('[%s]', paste(dQuote(strsplit(gsub("[][]", "", input_text), 
        ",\\s*")[[1]], FALSE), collapse=", "))

-output

[1] "[\"innovation manager\", \"manager director\", \"senior manager\"]"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading