Extract text from SMS and show it on a dialog box

Lewis8973

New member
Hey, I have made several SMS commands to replace the functions of the once good Avast and AndroidLost apps. This one I'm stuck on.

I'm trying to get part of my SMS to display in a dialog. I think I have the actions mostly right, but the macro doesn't activate in tests, so I've done something wrong with the trigger.

The trigger I have is:
[v=SMSTriggerCharacter]NOTIFY[v=SMSTriggerCharacter](\d+)[v=SMSTriggerCharacter]

The character is #, so I would ideally like #NOTIFY#Message Here# to display the Message Here in a dialog box. The regex thing I copied from a different working macro I have and hoped it would work, but it hasn't.

What can I do here please? Thanks.
 

Attachments

  • Screenshot_20211202-171832_MacroDroid.jpg
    Screenshot_20211202-171832_MacroDroid.jpg
    284.7 KB · Views: 32
  • Screenshot_20211202-171722_MacroDroid.jpg
    Screenshot_20211202-171722_MacroDroid.jpg
    405.5 KB · Views: 31
  • Screenshot_20211202-171713_MacroDroid.jpg
    Screenshot_20211202-171713_MacroDroid.jpg
    436.9 KB · Views: 29

tanutanu

Well-known member
Hey, I have made several SMS commands to replace the functions of the once good Avast and AndroidLost apps. This one I'm stuck on.

I'm trying to get part of my SMS to display in a dialog. I think I have the actions mostly right, but the macro doesn't activate in tests, so I've done something wrong with the trigger.

The trigger I have is:
[v=SMSTriggerCharacter]NOTIFY[v=SMSTriggerCharacter](\d+)[v=SMSTriggerCharacter]

The character is #, so I would ideally like #NOTIFY#Message Here# to display the Message Here in a dialog box. The regex thing I copied from a different working macro I have and hoped it would work, but it hasn't.

What can I do here please? Thanks.
Did the trigger fire? Check it up in your system log. \d in regexp means numeric characters as same as "[0-9]" character class.
If you want to make it match any characters, use ".*" or ".+"
 

Lewis8973

New member
No it didn't fire, nothing in the system log. I did however, after some playing around with your message, change both the trigger and text extraction to (.*), which now works, however the dialog box/local variable keeps the entire SMS command, not just the message between the hash trigger characters
Did the trigger fire? Check it up in your system log. \d in regexp means numeric characters as same as "[0-9]" character class.
If you want to make it match any characters, use ".*" or ".+"
 

tanutanu

Well-known member
No it didn't fire, nothing in the system log. I did however, after some playing around with your message, change both the trigger and text extraction to (.*), which now works, however the dialog box/local variable keeps the entire SMS command, not just the message between the hash trigger characters
oops, ".*" or ".+" matches as long as it can. try ".*?" or ".+?" instead. The additional character, "?", means the matching length is limited prior to the next "#"
 

Lewis8973

New member
Hey, I tried changing them both to (.*?), but now the dialog isn't showing anything. The text portion where it should pull the variable is empty, as is the list of variables at the bottom of the macro page. The logs look normal
oops, ".*" or ".+" matches as long as it can. try ".*?" or ".+?" instead. The additional character, "?", means the matching length is limited prior to the next "#"
 

tanutanu

Well-known member
Hey, I tried changing them both to (.*?), but now the dialog isn't showing anything. The text portion where it should pull the variable is empty, as is the list of variables at the bottom of the macro page. The logs look normal
Does your text for the testing really contain "#" as the last delimiter?

"#NOTIFY#.*?#" or "#NOTIFY#.+?#";
matches "#NOTIFY#foobar#hogehoge"
but does NOT match "#NOTIFY#foobarhogehoge"
 

Dm114

Well-known member
Hey, I tried changing them both to (.*?), but now the dialog isn't showing anything. The text portion where it should pull the variable is empty, as is the list of variables at the bottom of the macro page. The logs look normal
Use regex below and tick 'Group 1'
#NOTIFY#(.+)#
 

tanutanu

Well-known member
The best practice extracting at least 1 character between 2 different patterns is "(?<=patternA).+(?=patternB)" or "(?<=patternA).+?(?=patternB)"
It works when you use 2 or more groups as well. The "?" after "+" needs depending on the context, "?" is required for the first patternB.
 

tanutanu

Well-known member
I'm not quite sure what you mean, but my current format is
[v=SMSTriggerCharacter]NOTIFY[v=SMSTriggerCharacter](.*?)[v=SMSTriggerCharacter] where the variable is a #


Hey, I did try this but the whole SMS message is being displayed
For your case;
(?<=[v=SMSTriggerCharacter]NOTIFY[v=SMSTriggerCharacter]).*?(?=[v=SMSTriggerCharacter])
NOTE: It matches any character(s) including zero length.

[v=SMSTriggerCharacter] is equal to "#"
PatternA is "#NOTIFY#" and PatternB is "#"
So, you can extract the string between A and B.

If you change your mind to apply 2 or more keywords, you can modify it like that;
(?<=#(NOTIFY|WARNING)#).+?(?=#)

If you use the expression, "#NOTIFY#(.*?)#" and apply the group 1 option to "(.+?)", it wouldn't work in this case because it should be group 2 but MD has no group 2+ option.
 
Last edited:

Lewis8973

New member
For your case;
(?<=[v=SMSTriggerCharacter]NOTIFY[v=SMSTriggerCharacter]).*?(?=[v=SMSTriggerCharacter])
NOTE: It matches any character(s) including zero length.

[v=SMSTriggerCharacter] is equal to "#"
PatternA is "#NOTIFY#" and PatternB is "#"
So, you can extract the string between A and B.

If you change your mind to apply 2 or more keywords, you can modify it like that;
(?<=#(NOTIFY|WARNING)#).+?(?=#)

If you use the expression, "#NOTIFY#(.*?)#" and apply the group 1 option to "(.+?)", it wouldn't work in this case because it should be group 2 but MD has no group 2+ option.
Thanks for your help but I'm getting a bit confused now. I tried the first thing, and put ".*?" in the "Text to match" of the text manipulation action, as well as variations of that, and I'm still getting either blank dialogs, or the full message
 

Attachments

  • Test_-_Show_notification.macro
    2.8 KB · Views: 2

tanutanu

Well-known member
Thanks for your help but I'm getting a bit confused now. I tried the first thing, and put ".*?" in the "Text to match" of the text manipulation action, as well as variations of that, and I'm still getting either blank dialogs, or the full message
You don't need the last "?" in the settings of Extract Text action. Simply omit to get the text entirely and put it into the variable. If you want to cut the delimiters and keyword, set the same expression as in the trigger.

In this context, ".*?" indicates it matches as short as possible, while ".*" do as long as it can.
For example, the string, "#NOTIFY#foobar#";
".*#" matches the string entirely because the last "#" matches the expression,
".*?#" matches the zero length string because the first "#" matches.
".*" means any continuous character(s) zero length or longer, while ".+" means any at least 1 character as you know.

Back to the explanation in Extract Text action, now your expression, ".*?" have no character after "?", so it matches any character(s) including zero length before empty character. Therefore, it returns ""(zero length character/empty).
 

Attachments

  • Screenshot_20211204-034111~2.png
    Screenshot_20211204-034111~2.png
    98.9 KB · Views: 8
Last edited:

Dm114

Well-known member
I'm not quite sure what you mean, but my current format is
[v=SMSTriggerCharacter]NOTIFY[v=SMSTriggerCharacter](.*?)[v=SMSTriggerCharacter] where the variable is a #


Hey, I did try this but the whole SMS message is being displayed
If your SMS really looks like what you described (i.e. #NOTIFY#The text of the SMS#) the regex I gave you works fine. But, maybe, it doesn't have exactly this format... 🤔
 
Top