2

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?

3

You can do this with regex in Shortcuts.

Here with text: enter image description here

and here with integers: enter image description here

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 (.+),␣):

enter image description here

  • Hi the above regex only works on lines that have 1 word. How can I get regex to work on lines that have 1 or more words? – luciano Apr 22 at 15:59
  • @luciano What are the separator of words? Always a space, or can it be any other punctuation sign? Have you "words" like ora4nge in the input (mix of letters and digits)? – quark67 Apr 22 at 18:16
  • Can be any punctuation sign. Words include letters only. – luciano Apr 23 at 7:40
  • @luciano Does my updated answer satisfy your requirement? – quark67 Apr 23 at 20:42
  • Hi that is very nearly what I'm after, but each line should fall within 1 pair of quotes. Please see my edited question. Thanks! – luciano Apr 24 at 21:21
-1

We want the equivalent of this piece of JS code:

"c(" + input_text.split('\n').join(', ') + ")"

So the script will be something like this:

Split Text (New Lines) -> Combine Text (Custom separator ", ")

Integrate as you wish with the rest of the script.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .