Regular expressions

667 views
Skip to first unread message

Sean Clair

unread,
Nov 27, 2015, 10:10:20 PM11/27/15
to Automate
I have a flow that will open up a list of Wi-Fi ssid I trust, the scanning all wifis in area into kw then checking to see if any of kw matches what's on list if so it will connect to that. The reg expression I'm attempting is:

matches("{kw}", ["{known}"])


Where kw is the array of current ssid I'm seeing and known is the list if there is a match I want it to connect.

Thanks for any help

Sean Clair

unread,
Nov 27, 2015, 10:14:34 PM11/27/15
to Automate

Henrik Lindqvist

unread,
Nov 27, 2015, 10:53:52 PM11/27/15
to Automate
Try:
matches(kw, "(?:first|second|third)")

The next release, currently in Alpha, will have an intersect function:
intersect(kw, ["first", "second", "third"])

Sean Clair

unread,
Nov 27, 2015, 11:09:31 PM11/27/15
to Automate
So with first second and third I would need to have ssid as those there's no way to automatically write them once I deem them trusted and connect if they are in a file

Henrik Lindqvist

unread,
Nov 27, 2015, 11:49:17 PM11/27/15
to Automate
You could construct the regex from an array (known) like this:
matches(kw, "(?:{known;join;|})")

Sean Clair

unread,
Nov 27, 2015, 11:53:51 PM11/27/15
to Automate
Thanks I will try these both.

Sean Clair

unread,
Nov 30, 2015, 6:28:59 PM11/30/15
to Automate
Perhaps I do not understand exactly how to implement this, but I have had issues with deploying what you guys have said over the past few days.  I have tried loading SSID in area into a variable, test that variable against an array that I've made, (or a variable i've made, as the array functions are lost on me) and are not allowing me to call it line by line.  I mean to say I can call all SSIDs in area, can see SSIDs on my file, but when I try matches, and reg expressions, it seems to be failing to see theres a match.

Would JSON be a better tool to use for this job, or am I just making some sort of amateur mistake.   Thanks guys.

Henrik Lindqvist

unread,
Nov 30, 2015, 7:27:50 PM11/30/15
to Automate
Try using the Alpha version instead, it has new function called intersect which will help:
https://plus.google.com/110165001747691845730/posts/ZgetBzG9Kdt
  1. If you've stored all SSIDs in a text file, where each line is a name. Use the File read text block to load it into a variable called ssids.
  2. Convert the file text content to an array of names using the Variable set block with variable ssids value: split(ssids,"\n")
  3. Use the Wi-Fi scan block to get nearby networks, assign the array of SSIDs to variable nearby
  4. To find out if any of the SSIDs (from the file) is nearby, use the Expression true block with expression: intersect(ssids, nearby)

Sean Clair

unread,
Nov 30, 2015, 9:46:51 PM11/30/15
to Automate
Thanks I'll try it tonight

Sean Clair

unread,
Dec 1, 2015, 9:13:10 PM12/1/15
to Automate
I'm having a hard time figuring this out still.  What I have tried.

As you have suggested, I first loaded the text file into an array, which the document is already separated by spaces:
wifi1
wifi2
wifi3

I can toast this list and it comes out great.  I then did as you suggested and split this list using split(kw, "/n") where kw is the list of known wifi 

I did the same for the SSID but ran into a problem, trying to turn that into a list like the above list, no reg expression I found online worked to split the list into:
wifi1
wifi2
wifi3

and instead was just in sentence form despite my efforts.

At the end of the flow I had it do intersect( kw, nearbywifi) with the expression true block, and had it say success if match, and fail if no match. Despite my home wifi being on this list it is not matching, which may be contributed to the lack of "/n" separated list.

Also, when im splitting, working with variables I do not know when to use {} and when to just use variable name.  I tried both expecting one to work and found that it made no difference but im sure there's a difference I  did not notice.

Also when I load a list like my trusted wifi list into an array, how can I call one specific line of the array.  I expected kw[0] to return the first in the list, but instead it returned weird numbers seemingly not even related to my list, like 55 which isn't even in the wifi names.  Im sure I just don't understand something but currently this is not quite working.

Thanks in advance!

Henrik Lindqvist

unread,
Dec 1, 2015, 9:41:34 PM12/1/15
to Automate
You must use backslash \ not divide /:
split(kw, "\n")

See the documentation:
http://llamalab.com/automate/doc/value.html#text

Sean Clair

unread,
Dec 1, 2015, 10:50:20 PM12/1/15
to Henrik Lindqvist, Automate

Yes I'm sorry I made a typo.  But what it returns from Wi-Fi available is a series of Wi-Fi in a comma separated list as in,

X wifi, y Wi-Fi and so on

When I try to do intersect with this list out doesn't find match


--
You received this message because you are subscribed to a topic in the Google Groups "Automate" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/automate-user/f6UNKBYmmUY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to automate-use...@googlegroups.com.
To post to this group, send email to automa...@googlegroups.com.
Visit this group at http://groups.google.com/group/automate-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/automate-user/03bec113-9f78-453c-ac8f-a408dc0cd5a0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Henrik Lindqvist

unread,
Dec 1, 2015, 11:09:45 PM12/1/15
to Automate, henrik.l...@gmail.com
When you convert an array to text it done so with commas between adjacent elements.

Use the Variable set block
Variable: kw
Expression: split(fileContent, "\n")

After that you can access the kw array using the subscript operator:
kw[0]

...or, in your case use the intersect function in an Expression true block with the array of nearby SSIDs:
intersect(kw, nearby)



On Wednesday, December 2, 2015 at 4:50:20 AM UTC+1, Sean Clair wrote:

Yes I'm sorry I made a typo.  But what it returns from Wi-Fi available is a series of Wi-Fi in a comma separated list as in,

X wifi, y Wi-Fi and so on

When I try to do intersect with this list out doesn't find match


Sean Clair

unread,
Dec 1, 2015, 11:17:19 PM12/1/15
to Henrik Lindqvist, Automate

Why is it when I try to call {trustedwifi[0]} it returns 55 which is not the first entry in file nor does it exist at all,  thanks again for your help, I appreciate it


--
You received this message because you are subscribed to a topic in the Google Groups "Automate" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/automate-user/f6UNKBYmmUY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to automate-use...@googlegroups.com.
To post to this group, send email to automa...@googlegroups.com.
Visit this group at http://groups.google.com/group/automate-user.

Henrik Lindqvist

unread,
Dec 1, 2015, 11:35:28 PM12/1/15
to Automate, henrik.l...@gmail.com
55 is the codepoint of the first character of the text value in variable trustedwifi
Make sure the trustedwifi variable really is an array. If you do as i've explained it should be an array.



On Wednesday, December 2, 2015 at 5:17:19 AM UTC+1, Sean Clair wrote:

Why is it when I try to call {trustedwifi[0]} it returns 55 which is not the first entry in file nor does it exist at all,  thanks again for your help, I appreciate it


Sean Clair

unread,
Dec 2, 2015, 10:03:22 AM12/2/15
to automa...@googlegroups.com, henrik.l...@gmail.com
Is there a way to confirm it's an array?  I have done as I've said, flow looks like this:

begin flow
    |
read file "known.txt" output variable ssid
    |
set variable tw to split (ssid, "/n")
    |
when I toast this at {tw[0]} I get the whole "array" or the whole file as in x, y, z on new lines each.  If I avoid splitting and toast I get the same thing which makes me wonder.  If I alternatively do {tw[1]} I get an empty space toasted.

    |
await wifi scan output variable nearby
    |
set variable split (nearby, "/n")
    |
expression true = insert(nearby, tw)  Which returns false indicating there is no match between the two supposed arrays.

Is there a way to confirm I have an array?

Henrik Lindqvist

unread,
Dec 2, 2015, 2:20:07 PM12/2/15
to Automate, henrik.l...@gmail.com
Use the type function to get the value type of an variable: type(tw)

Again, it MUST be backslash, not divide:  split(ssid, "\n")
The nearby variable, the array of SSIDs as returned by the Wi-Fi scan block, is already an array so you do not need to use split.

Sean Clair

unread,
Dec 2, 2015, 5:00:29 PM12/2/15
to Automate
Forgive me for typing that wrong. I wish there was a way to directly share the flow to you here. I used the type on the split on the kw variable and found it was am array but when I try to call issued kw[0] it returns whole file, when I do kw[1] or above it returns nothing.

Is there a regular expression to use with split that would make index after each comma?

Sean Clair

unread,
Dec 2, 2015, 5:04:09 PM12/2/15
to Automate
I was able to see the local Wi-Fi and was able to toast it by taking away {} so nearby[1] does show second Wi-Fi in array. Now to see if intersect will work!

Henrik Lindqvist

unread,
Dec 2, 2015, 6:19:34 PM12/2/15
to Automate
Curly brackets are used for including variables in text "foo{var}bar" instead of writing "foo"++var++"bar".
Automate has proper value types (text, number, array, dictionary), not everything is text like in Tasker and Automagic.

Sean Clair

unread,
Dec 3, 2015, 6:17:52 PM12/3/15
to Automate
Thanks for your assistance.
Reply all
Reply to author
Forward
0 new messages