# Put in the quotes

## Background

There's a terrible problem in my console - the quotes never get included inside the arguments! So, when given this argument:

["abc","def","ghi","jkl"]


it says that the argument is like this:

[abc,def,ghi,jkl]


It would be very nice if you can fix this problem!

## Challenge

Add double-quotes (") in order to surround a word (i.e. something that matches [a-z]+).

## Test cases

[[one, two, three], -> [["one", "two", "three"],
[one, two, three],     ["one", "two", "three"],
[one, two, three],     ["one", "two", "three"],
[one, two, three]]     ["one", "two", "three"]]

[abc,def,ghi,jkl] -> ["abc","def","ghi","jkl"]

this is a test    -> "this" "is" "a" "test"

test              -> "test"

this "one" "contains' "quotations -> "this" ""one"" ""contains"' ""quotations"

But This One Is SpeciaL! -> B"ut" T"his" O"ne" I"s" S"pecia"L!


## Rules

• The inputted words are never capitalized. Capital letters are not considered part of a word.
• The input is always a string, not a list. The test-cases aren't quoted because that's the only possible type.
• A word is something that matches [a-z]+.
• Sandbox
– user92069
Feb 23 '20 at 14:59
• Don't understand how the rule: The inputted words are never capitalized. works with the test case: But This One Is SpeciaL! -> B"ut" T"his" O"ne" I"s" S"pecia"L! Feb 23 '20 at 16:20
• I don't get how the lists of lists in the first code block relates to the challenge.
– xnor
Feb 23 '20 at 16:26
• Some of the test case inputs are strings, some are arrays. Is it our choice which to support? It's also unclear what the first block quoted example of the array [one,two,three] repeated 4 times is supposed to demonstrate. Feb 23 '20 at 17:15
• @Jonah I interpreted the first test case [abc,def,ghi,jkl] to be a string that happens to look like an array (the first character of the string is '[', etc.). Feb 23 '20 at 19:12

# Gema, 8 characters

<J>="$0"  Copy of Adám's QuadR solution, just adjusted the syntax and the explanation: Replace <J> one or more lowercase letters with "$0" the quoted match.

Sample run:

bash-5.0$gema '<J>="$0"' <<< '[abc,def,ghi,jkl]'
["abc","def","ghi","jkl"]


Try it online!

s/[a-z]+/"$&"/g  # QuadR, 10 bytes [a-z]+ "&"  Try it online! Replace [a-z]+ one or more lowercase letters with "&" the quoted match. # Japt, 11 bytes r"%a+""$&"


Try it

• I never knew "$&" existed, I was trying "$0", because C# does that and I assumed other languages would too. When that didn't work, I thought Japt doesn't have that built in. But you learn something new each day! Feb 24 '20 at 6:11

# sed -E, 2015 14 bytes

Saved 5 bytes thanks to @Neil! And then 1 more byte thanks to @manatwork pointing out that we no longer have to add bytes to our score for command-line options.

s/[a-z]+/"&"/g


Original solution:

s/$$[a-z]\+$$/"\1"/g


Test run:

$cat quotelc.sed s/$$[a-z]\+$$/"\1"/g$ sed -f quotelc.sed << EOF
> [abc,def,ghi,jkl]
> this is a test
> test
> this "one" "contains' "quotations
> But This One Is SpeciaL!
> EOF
["abc","def","ghi","jkl"]
"this" "is" "a" "test"
"test"
"this" ""one"" ""contains"' ""quotations"
B"ut" T"his" O"ne" I"s" S"pecia"L!


Note: In TIO's sed, there's some issue with the collating sequence; the pattern [a-z] matches upper-case letters too. If you want to run this in TIO, you can do something like this.

• s/[a-z]\+/"&"/g saves 5 bytes, I think?
– Neil
Feb 24 '20 at 0:32
• Thanks, @Neil! I haven't golfed for a while, and I forgot about '&' in sed. Feb 24 '20 at 4:41
• Since command line arguments are for free, with GNU sed you can specify --regexp-extended (or -r) to not need to escape the + quantifier. Feb 24 '20 at 8:59
• @manatwork : I've understood that you need to add the number of bytes in any additional options. That would be 3 bytes here (including the space before the option), which wouldn't be worth it. (By the way, grep has a standard POSIX option for extended regexes now: -E instead of the old -r. So if you want to do that, -E is more portable.) Feb 24 '20 at 9:11
• They're free, but it's considered a different language. Mar 16 '20 at 23:29

# Python 3, 47 bytes

lambda s:re.sub('([a-z]+)',r'"\1"',s)
import re


Try it online!

# Jelly, 15 bytes

Ż;0e€ØaIkƊj”"ḊṖ


Try it online!

A monadic link taking a Jelly string and returning a Jelly string. No regex in Jelly so identifies boundaries between lower letter segments and others, splits and joins with quotes.

# Charcoal, 20 bytes

Ｆ⁺Ｓ «ω✂"⁼№⪪β¹ω№βι≔ιω


Try it online! Link is to verbose version of code. I was stuck at 24 bytes for some time until I came up with this approach. Explanation:

Ｆ⁺Ｓ «


Loop over the input with a space appended. (Any non-alphalower character would do.)

ω


Print the previous character. On the first pass through the loop, this variable holds the empty string, so nothing is output. Additionally, we never print the appended space, as it's never the previous character.

✂"⁼№⪪β¹ω№βι


Check whether both or neither the previous or current characters are alphalower characters. If exactly one is, then output a quote. (The predefined alphalower string has to be split for the first check otherwise the empty string would match 27 times on the first pass.)

≔ιω


Save the current character so that it's the previous character on the next pass of the loop.

# Retina, 11 bytes

[a-z]+
"$0"  Pretty straight-forward approach. Try it online. # J, 39 35 bytes load'regex' f=.'[a-z]+'dquote rxapply]  Try it online! -4 bytes thanks to Bubbler teaching me about dquote builtin NOTE: The TIO link is showing an error, but this works correctly on my machine (j807), and passes the test cases. I'm unsure if it's a version issue or something else causing TIO to fail. Task is accomplished almost entirely by the rxapply adverb. We simply have to supply the regex to apply to [a-z]+ and the verb to apply: surround in quotes dquote • Use dquote stdlib verb instead of ('"',,&'"') for 35 bytes (regex lib still not working). Feb 27 '20 at 9:55 • Thanks @Bubbler. TIL... Feb 27 '20 at 13:16 # 05AB1E, 14 13 bytes 0.ø.γ.l}'"ý¦¨  Try it online! # Husk, 10 bytes ṁ?sIΛ√ġo¬√  Try it online! # Deorst, 20 bytes '"\1"'@ '([a-z]+)'gs  Try it online! Again, just uses a regex based approach # PHP, 69 bytes for(;$c=$argn[$i++];){if(($n=$c>''&$c<'{')^$p)echo'"';echo$c;$p=$n;}  Try it online! # Japt, 12 bytes r"%a+"@Q+X+Q  Try it • Sorry, edited your solution, thinking it was mine and that I'd somehow managed to post the wrong one! It's been a long weekend! Feb 23 '20 at 20:17 # Jelly, 13 bytes Ø1je€ØanƝ”"ẋż  A full program which prints the result. Try it online! Or see the test-suite. ### How? Ø1je€ØanƝ”"ẋż - Main Link: list of characters e.g. Foo 'Bar', baz -> ['F','o','o',' ',"'",'B','a','r',"'",',',' ','b','a','z'] Ø1 - ones [1,1] j - join [1,'F','o','o',' ',"'",'B','a','r',"'",',',' ','b','a','z', 1] Øa - lower-case alphabet ['a','b',...,'z'] € - for each: e - exists in? [0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0] Ɲ - for neighbouring pairs: n - not equal? [ 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1] ”" - quote character '"' ẋ - repeat (vectorises) [ [],['"'],[],['"'],[],[],['"'],[],['"'],[],[],['"'],[],[],['"']] ż - zip together [[[],'F'],[['"'],'o'],[[],'o'],[['"'],' '],[[],"'"],[[],'B'],[['"'],'a'],[[],'r'],[['"'],"'"],[[],','],[[],' '],[['"'],'b'],[[],'a'],[[],'z'],[['"']]] - implicit, smashing print F"oo" 'B"ar"', "baz"  # Burlesque, 32 bytes J"[^a-z]+"~?[~jqup"[a-z]+"~aj.+Q  Try it online! Depressingly, more than half of this is attempting to overcome what I can only assume is a bug where the ~a cuts off the last non-match. J # Duplicate "[^a-z]+"~? # All matches for not a-z [~ # Take the last one j # Swap # Main block (13 bytes) qup # Unparse to string "[a-z]+" # Matches a-z ~a # Apply to those that match regex #### j.+ # Add on hacky fix Q # Pretty print  # PHP, 43 41 bytes <?=preg_replace('/[a-z]+/','"$0"',$argn);  Try it online! A simple straightforward regex implementation EDIT: Thanks to @manatwork for saving 2 bytes! • There you capture the entire matched substring into $1, so it contains the same as $0. So better use that one and that way you can remove the capturing (..). Feb 26 '20 at 10:13 # JavaScript (V8), 33 bytes f=i=>i.replace(/[a-z]+/g,'"$&"');


-9 Bytes thanks to manatwork

Try it online!

• Why the callback? '"$&"' is shorter than e=>"\""+e+"\"". (But if you really want to use callback, e=>'"'+e+'"' is shorter. Not mentioning e=>"${e}".) Feb 27 '20 at 9:32
• Same moment you wrote the shorter callback version i wanted to update to this solution too. And yes, first one is even shorter.. wasn't able to produce this due to errors I got.. update to post incoming :D Feb 27 '20 at 9:36