r/tasker•
rfn0

Search between 2 words

Please forgive me for asking this question.

I know the answer requires the use of regex

But can anybody explain it to me in simple words: I need to get word(s) between two other words from a text. How do I do it?

Non regex answers also would be helpful.

My knowledge about regex is very little. Thank you.

Posted Aug 27, 2020, 3:07 AM UTC

513
moviejimmy•

Let's say you have a variable, %text, as below:

I know the answer requires the use of regex

And you want to extract everything between "know" and "use":

Variable Search Replace action
Variable: %text
Search: know.*?use
Store Matches in Array: %match

After running the above, %match1 will contain:
know the answer requires the use

3
rfn0OP•

I'll check this and come back Thank you

1
rfn0OP•

Oh Yes!

This works

Now what if I want only the "central part". Not "know" and "use"

Thanks in advance

1
rfn0OP•

For now, I used a second and third variable search and replace functions to replace the initial and final parts with "null".

Works!!!

Thank you But if, there is a direct way to do it, I would be happy.

1
moviejimmy•

Search: (?<=know).*?(?=use)

2
rfn0OP•

Thank you

1
agnostic-apolloLG G5, 7.0 stock, rooted•

Firstly, there is no forgiveness here, only retribution.

Secondly, if u just want to remove first and last words and want to get any words in between just use the regex ^\w+ | \w+$ in Variable Search Replace action and enable Replace Matches toggle and leave Replace With field empty. Your original variable will have the first and last words removed.

The ^ in regex means start of string,$ means end of string, | means OR, \w+ means one or more word characters. Basically match against one or more word characters at start of string, followed by one space character OR match against one space character followed by one or more word characters at end of string.

You haven't really provided much details about your input string, so add that if u want better help.

2
rfn0OP•

Thank you

If you don't mind, can we use the same example as before.

"I know the answer requires the use of regex."

In this sentence above, how do I use

  1. Include the words "know" and "use" and include everything in between
  2. Exclude the words "know " and "use" but include everything in between

This would really help me in understand regex too.

1
Gianckarlo•

I know the answer requires the use of regex

  1. Just use know.+use.
  2. Use (?<=know ).+(?= use). Positive/Negative Lookbehind/Lookahead are extremely useful. Try to learn to use them.

Also, you can test your regex more easily in this page.

3
agnostic-apolloLG G5, 7.0 stock, rooted•

That can be done with capturing groups in regex. Anything you surround within the first round brackets in a regex can be referred as $1 in the replace field, the second capturing group with $2 and so on.

  1. Use ^.*(know.*?use).*$ and replace with $1

  2. Use ^.*know(.*?)use.*$ and replace with $1

For info on .* and .*?, check here.

2
rfn0OP•

Thank you so much This was really helpful.

Looks like you really know regex. Do you mind if I ask you a few more doubts if and when I have, on the subject.

1
agnostic-apolloLG G5, 7.0 stock, rooted•

You are welcome.

Yeah, I use them a lot in my daily life. Sure, will try to help out in my free time.

2