RegEX and Accessibility help

maidchan

New member
Hi,

I had two questions. The first is that frequently my accessibility options revokes macrodroid permissions randomly, and I have to re-enable them. Is there any way to prevent this?

Regarding Regex, I'm trying to write an regex that detects if in a string of numbers, a digit is repeated twice exactly. However, I don't want it to trigger on triple repeats etc.

So something like 991999 (five 9s) would not be triggered, but 910789 (two nines) would.

Thank you for any help.
 

RSF

Well-known member
Re: accessibility: yes, this happens periodically to me as well (and not just with MacroDroid, but other apps). I get the sense there's issues with Android. MacroDroid's developer added a helpful action recently which restores accessibility settings -- Device Settings / Accessibility Service. Note that you'll need the "ADB Hack" active on your phone for that to work. You could set up a macro to run once or twice a day to check the Secure Setting enabled_accessibility_services and if it's missing your app, call that new MacroDroid action to restore it.

Re: regular expression for digits occurring twice: if you're specifically looking for 9's, you could use
^[^9]*9[^9]*9[^9]*$
If you're looking for any digit to occur twice, but nor more than twice, all with a single regex, you could use
/^(([^1]*1[^1]*1[^1]*)|([^2]*2[^2]*2[^2]*)|([^3]*3[^3]*3[^3]*)|([^4]*4[^4]*4[^4]*)|([^5]*5[^5]*5[^5]*)|([^6]*6[^6]*6[^6]*)|([^7]*7[^7]*7[^7]*)|([^8]*8[^8]*8[^8]*)|([^9]*9[^9]*9[^9]*)|([^0]*0[^0]*0[^0]*))$
which is ugly but should work. As a variation on that very long regex, you could put the 10 variations of the first regex (one per digit 0-9), each defined as a condition on an "IF" action (or constraint) using the "OR" conjunction. Still long and clunky.

Alternatively you could loop through the string a digit at a time, incrementing dictionary or array entries keyed by the digit, then at the end, loop through the dictionary/array, making sure there's an entry with a 2 but no entry higher than a 2. That's more flexible (could work for characters other than digits, for example, and for finding three vs. occurrences, etc.) but requires more actions.
 

maidchan

New member
I see, that one is quite long but it does seem to work on regex101. Thank you very much I'll try to fit into macrodroid, hopefully theres no max character limit
 
Top