Split Variable

Ian Moone

New member
Hi,

Can anyone tell me how I can get a part of a variable? (or maybe a work around)

For example:
Variable: 12345-67890
Var1 = 12345
Var2 = 67890

Var1 = from beginning to "-"
Var2 = from "-" to end
Next time the lenght of Var1 or Var2 can be different.
So i can't use Substring Start and End index.

Thank you.
 

tanutanu

Well-known member
use regexp:)
 

Attachments

  • Screenshot_2021-02-27-03-08-48-663_com.arlosoft.macrodroid.jpg
    Screenshot_2021-02-27-03-08-48-663_com.arlosoft.macrodroid.jpg
    344.7 KB · Views: 85
  • Screenshot_2021-02-27-03-09-44-969_com.arlosoft.macrodroid.jpg
    Screenshot_2021-02-27-03-09-44-969_com.arlosoft.macrodroid.jpg
    346.6 KB · Views: 76

Ian Moone

New member
Thanks to the both of you.

The first one works perfectly.
Now i've got one a little bit more challenging.
Still haven't figured out how regex really works, but i get there.
 

tanutanu

Well-known member
Thanks to the both of you.

The first one works perfectly.
Now i've got one a little bit more challenging.
Still haven't figured out how regex really works, but i get there.
Simply delete the last letter $.
It means the end of a line but not required for your case:)
regex is a little complicated for beginner but you can do it.
 

Dm114

Well-known member
Hi,

Can anyone tell me how I can get a part of a variable? (or maybe a work around)

For example:
Variable: 12345-67890
Var1 = 12345
Var2 = 67890

Var1 = from beginning to "-"
Var2 = from "-" to end
Next time the lenght of Var1 or Var2 can be different.
So i can't use Substring Start and End index.

Thank you.
If your pattern is as simple as in your example, you even can use a much simpler regex expression as shown below with separator (dash in your example)
- either at the end, to extract any number of digits before it
- or seperator as the very first character of the expression to extract any number of digits after it.
 

Attachments

  • 20210227_155224.jpg
    20210227_155224.jpg
    90.6 KB · Views: 36

Ian Moone

New member
Thanks for the suggestions.

I found a different way by using Regex101.com.

Example: 12a345-67b890-54c321

(?<=-).* -> 12a345
(?<=-).*(?=-) -> 67b890
.*(?=-) -> 54c321


I only have to use the first and last regex-code and it includes other characters as well.
Still working on a regex-code when there are more fields to extract.
 
Last edited:

the-unknown

New member
Hallo Guys,

I don't understand this, the classical split variable is much easier for me (as in Tasker).

I think I am making a stupid mistake.

I have the global variable %Artist which contains a text like the following:
MyMTCService: mediaController -- android.media.metadata.ARTIST=Wir sind der Westen

This text i get from a logcat entry but I only need the part after the = (in this Case "Wir sind der Westen")

How can this be done?

Tried it with the command shown in this post

But I only get the name of the new variable.

Thanks in advance

Christoph
 

RSF

Well-known member
Christoph -
Try a regular expression of
(?>.*ARTIST=)(.*)
and tell MacroDroid to use the "Group 1" option in the Text Manipulation > Extract Text action.
ArtistExtract.png
 

RSF

Well-known member
Ian -
  1. Append a hyphen to your string (will make regex parsing easier for the last element e.g. 12a345-67b890-54c321- since every element is followed by a hyphen that way)
  2. Use regex
    /(?>[^-]+-){n}([^-]+)/
    where you replace the blue "n" with the number of matches to skip. So, 0 for the first element, 1 for the second, 2 for the third... And use MacroDroid's "Group 1" in the extract text action.
 

Ouitoo

New member
thanks, a really simple command if you know whats the command ;)
I'm with you on this one. I had never encountered regular expressions until I started messing around with MacroDoid and Tasker. And what's with the name because they are far from regular! It looks like the least user friendly thing I have seen in a long time. Are we heading back to assembler language next? Haha but seriously I do wonder why we don't just have more powerful actions like parsing and substringing using separators. And arrays would be a huge help in Macrodroid also.
 

MacroDroidDev

Administrator
Staff member
Just to confirm what @Snurre said dictionary and array variable types are available for creation to pro users in the latest version of MacroDroid.
 
Top