Use Shortcuts app to format text I have text in this format:

Red and green apples
Pear
2 Bananas
red orange
Melon
Grape fruit


I need it in this format:

c("Red and green apples", "Pear", "2 Bananas", "red orange", "Melon", "Grape fruit")


I also have these numbers:

1
172
3
183
16
178
68
56
687
576
455


Which I need in the format below. Note that this is same format as above except they are numbers so they are missing quotes:

c(1, 172, 3, 183, 16, 178, 68, 56, 687, 576, 455)


Can I make a shortcut that will automatically convert the text?
You can do this with regex in Shortcuts.

Here with text:


and here with integers:


The code is the same, it’s only the first "Text" action which differ.
More about regex in Shortcuts in this reddit post.

Edit: do not confuse, in the two first "Find Text" in the two screenshots, the | in (\n|$) is the vertical bar character, not a uppercase "i" or a lower case "L".

Edit 2: As asked in the comments by the OP and in the edited question, here is a different version with less constraint in the input (remark: in the first Replace Text action, in the field Replace With, you should read "$1",␣, ie a space should be after the comma, but the screenshot cannot display it, and in the 3rd Replace Text, in the field Find Text, you should read (.+),␣):