Detect number in a string.

Post your questions and help other users.

Moderator: Martin

Post Reply
robchoc
Posts: 81
Joined: 20 Jun 2018 12:38

Detect number in a string.

Post by robchoc » 07 Dec 2018 13:14

Is there a simple way to detect for any number in a string or a way to compare 2 strings and if they are different play a sound?

Example of a string that I'm using that has no numbers (I used getTextFromActiveWindow):

(how do I put all this text into a string?)

"STEERS
IN-STORE
We don’t have any steers for you to do right now. If you have a bad, or no internet connection, this can happen.
We don’t have any steers for you to do right now. If you have a bad, or no internet connection, this can happen.
vypr"

Here is one that has numbers that I want to detect and play a sound when they are found:

"STEERS
5
IN-STORE
10
POINTS
10
POINTS
10
POINTS
10
POINTS
10
POINTS
We don’t have any steers for you to do right now. If you have a bad, or no internet connection, this can happen.
vypr"

Thanks for any help.

anuraag
Posts: 371
Joined: 24 Jan 2015 02:06

Re: Detect number in a string.

Post by anuraag » 07 Dec 2018 14:16

This will give you all numbers
findAll('string', '\\d+')

To compare
Use expression
string1 != string2

robchoc
Posts: 81
Joined: 20 Jun 2018 12:38

Re: Detect number in a string.

Post by robchoc » 07 Dec 2018 15:34

And how would I put

"STEERS
IN-STORE
We don’t have any steers for you to do right now. If you have a bad, or no internet connection, this can happen.
We don’t have any steers for you to do right now. If you have a bad, or no internet connection, this can happen.
vypr"

into a variable to compare it against?


This is what I'm trying:

Code: Select all

sleep (1000);
text = getTextInActiveWindow();
sleep (1000);
comp = 0;
comp = findAll('text',  '\\d+') ;
Then in the expression condition, I'm using:

Code: Select all

comp == null
My thinking is that if comp does not contain a number then it will be true. If comp does contain a number it will be false and trigger my sound.

For some reason when there are no numbers it makes my expression false and triggers the sound. :?

Thanks
Last edited by robchoc on 07 Dec 2018 15:53, edited 1 time in total.

anuraag
Posts: 371
Joined: 24 Jan 2015 02:06

Re: Detect number in a string.

Post by anuraag » 07 Dec 2018 15:47

Put it in global variable.

Or
Replace line breaks with \n

Code: Select all

string2 = "STEERS\nIN-STORE\nWe don’t have any steers for you to do right now. If you have a bad, or no internet connection, this can happen.\nWe don’t have any steers for you to do right now. If you have a bad, or no internet connection, this can happen.\nvypr"

User avatar
Desmanto
Posts: 2689
Joined: 21 Jul 2017 17:50

Re: Detect number in a string.

Post by Desmanto » 09 Dec 2018 17:46

Fastest way to convert the line break to the proper escaped char (\n), is copy and paste it sample text in Regex tester at script/expression. Scroll down and copy the matches() or any other function for the first parameter. This will convert the unseen char too, including the tab (\t), which is always the best way to spot the invisible mysterius character.

The other way, if you are sure every line break is only \n, and you need the text to be easier to read, add the \n, but separate them with +. You have to do it manually.

Code: Select all

string =
"STEERS" +
"IN-STORE" +
"We don’t have any steers for you to do right now. If you have a bad, or no internet connection, this can happen." +
"We don’t have any steers for you to do right now. If you have a bad, or no internet connection, this can happen." +
"vypr" ;
I make a flow, just to create something like this. So I passed the text to the flow, and the output is just like above, double quoted at each line with plus sign to concat.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

robchoc
Posts: 81
Joined: 20 Jun 2018 12:38

Re: Detect number in a string.

Post by robchoc » 09 Dec 2018 19:26

Thanks

Was starting to get concerned about your whereabouts. Glad you are back. :)

User avatar
Desmanto
Posts: 2689
Joined: 21 Jul 2017 17:50

Re: Detect number in a string.

Post by Desmanto » 10 Dec 2018 05:04

:D Thanks for the concern. I'm fine, nothing bad happened. Just 2 weeks ago, I don't have much time to reply the thread for several days. Then I try to use that (the urge to reply the thread) to motivate me to reindex my thread and also finish my automagic vs tasker comparison. Turned out it didn't work. :( When I try to get back, there are already a lot to reply, so I postpone until I can find some time to reply most of them, and haven't look at every thread yet.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Post Reply