Jump to content

Splitting and reassembling text?


ttrop

Recommended Posts

I'm new to Alfred, and I'm trying to create a workflow that will take text from my clipboard and arrange it into a template. 

Basically what I am going to have is text in the format of a question, followed by 4 possible answers:

Question? a b c d

 

I want to split the text into two parts, the question and the answers, so if I can split at the "?" in the text that would be perfect. 

I then want to split the 2nd item (a b c d) into 4 parts (a, b, c and d)

I then want to reassemble the text so that it follows this template:

Question? a OR b OR c OR d

 

My thinking is that if I can set it up so that everything before the "?" becomes a variable {question} and then the 4 parts following the "?" become the variables {a}, {b}, {c} and {d}, I can then paste those variables into a template that looks like:
{question}? {a} OR {b} OR {c} OR {d}

 

Is this possible in Alfred? 

Thank you very much for your help!

Link to comment

Hi @vitor, thanks for responding. 

 

Ideally, this is how it should work:

-Starting text will look like this:

What is your favorite color?
Poppy Red
Sky Blue
Canary Yellow
Forrest Green

Everything before the "?" is the Question, and everything after is the Answers

-I would like to take that text, and modify it so it becomes:

What is your favorite color? Poppy Red OR Sky Blue OR Canary Yellow OR Forrest Green

 

I've figured out how to select everything before the "?" and everything after it, using regex expressions. 

 

To grab everything the before the question mark I've used: 

-Replace (regex) \?([^\?]+)$ with blank

-I then set that to variable = Question

 

To grab everything after the question mark I've used: 

-Replace (regex) ^[\s\S]+\? with blank

-I then set that to variable = Answers

 

Sometimes, the answers appear like this:



Poppy Red
Sky Blue
Canary Yellow

Forrest Green

So I've tried to remove the line breaks using:

-Replace (regex) /$\s*[\r\n]/gm with ' OR '

However, I can't get it to apply this to the previous 'replace' utility, and when I test it using RegExr.com it ends up looking like this: 

 OR Poppy Red OR Sky Blue OR Canary Yellow OR Forrest Green

 

So that leaves me with 2 questions for now. 

1) How do I string together multiple 'Replace' utilities, so that I can take the "Answers" section of the text and add in the " OR " 's ?

2) How do I make it so the " OR " is only added after the first non-blank line, so I don't end up with the first " OR " shown above?

 

-Thanks so much for your help!

 

Link to comment

Trying to do something that complex with Alfred Utilities is too time consuming and you’ll need to bend them to do things they’re not designed for. This is a task that benefits from a bit of coding. It can be done in just a few lines.


Download a ready-made Workflow here.

 

For reference, the code:

text = %x(pbpaste).squeeze("\n")

question = text.split("\n")[0]
answers = text.split("\n")[1..-1]

puts question + ' ' + answers.join(' OR ')

Link to comment

@vitor Thank you so much for your help. As I don't know how to code, your help is greatly appreciated!

I'm having one small problem with the workflow though. 

 

Out of the four following
colors which one of them
is your favorite?

Poppy Red
Sky Blue
Canary Yellow

Forest Green

is resulting in:

 

Out of the four following colors which one of them OR is your favorite? OR Poppy Red OR Sky Blue OR Canary Yellow OR Forest Green

The text I'm using is in the form of a multi-line question, with each answer on a new line after the "?"

Link to comment

@vitor I asked a friend with more knowledge on this topic than I have to lend a hand, and he came up with this: 

 

input = ARGV[0]

questions, answers = input.split('?')

puts questions.strip.gsub(/\n+/, ' ') + '? ' +
     answers.strip.split(/\n+/).join(' OR ')

That seems to do the trick for me!

 

Thanks again for your help!

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...