Page 1 of 1

Either RegExp is wrong or "matches=" is misunderstood

Posted: 07 Nov 2017 14:49
by husky
Hello,

I'm going in circles here trying to figure out why the code below does not work.

Code: Select all

 
//Regexp
^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](20|21)\d\d$

SCRIPT:
global_input_value="11/06/2017 12:34:26";

[color=#40FFFF]mmddyyyy[/color]=left(global_input_value,  length(global_input_value)- 9);  Here is the MM/DD/YYYY
hhmmss=right(global_input_value,  length(global_input_value)- 11);  Here is the HH:MM:SS

EXPRESSION:
resp=matches([color=#00FFFF]mmddyyyy[/color], "^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](20|21)\d\d$");
Using Regexp Checker the response is TRUE for Date!

There is something I do not understand.

When running under the Regexp tester, 11, 06, and 20 appears with underscore but 17 does not.
I do not know why the 17 (in 2017) is NOT underscored. However the overall return from the tester gives me --> true

group[0]=11/06/2017
group[1]=11
group[2]=06
group[3]=20
NO group for the 17. Why?

When I run the code above in the flow, and use Expression to test the response "resp" is always invalid. Changing MM => 31/06/2017 still (as expected) is invalid).

The Regexp as is, will not detect for example: 31/01/2018. Will work on later.

Thank You

Husky

Re: Either RegExp is wrong or "matches=" is misunderstood

Posted: 07 Nov 2017 15:12
by Desmanto
In Automagic, backlash is escape character, which need to be escaped as well. In regex tester, the backslash doesn't need to be escaped at all, since it is testing only.

When you have the matches, you will see the regex pattern has been changed a little, especially the part where you have backslash. You should follow this.
You can see that the last two \d\d changed to \\d\\d.
The easiest way is to just copy the part from the regex pattern from the matches and paste it to the script.

So instead of
^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](20|21)\d\d$
you should put
^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](20|21)\\d\\d$

17 is matched but not captured, since you don't put them in bracket. However, you can't put them in the same bracket as 20|21, that will break the group.
So you have to put another capture group for them, make it (20|21)(\\d\\d) => 17 will captured in group index [4]
or you can capture the whole, ((20|21)\\d\\d) => 2017 will be group[3], 17 will be group[4] (not needed anymore)

Re: Either RegExp is wrong or "matches=" is misunderstood

Posted: 07 Nov 2017 17:54
by husky
Hi Desmanto,

Thanks for the expedited reply.

FIRST:

Excellent. As we say around here and I assume you're not in the US, "the devil is in the details" in the form of "(" and "\\d \\d". These three guys threw me off the road. Crashed and Burned....

The flow now sends the correct response (true). I was getting dizzy running in circles here.

SECOND:

You compiled a lot of valuable answers and experiences in the link you sent me. Will take my time reading and learning.
Really a treasure trove with one difference, the author is known. Kudos to you.

I'll read the comments about AutoMagic and Tasker.
I tried Tasker some time back and found it powerful but not quite intuitive as AutoMagic and it's pricey having to buy the plugins.


Thank You very much


Regards

Husky

Re: Either RegExp is wrong or "matches=" is misunderstood

Posted: 08 Nov 2017 15:58
by Desmanto
Hi Husky,

Yes, English is not my primary language. I learn a lot of grammar just by posting and creating the article.
And thanks for the phrase, had to googled for more explanation about it :) Always learning something new everyday. That's why I am keep posting here.

I haven't update the index for a while. Had been distracted by my new toys, which I will integrate with automagic too (yeelight and sonoff). Too many things to play with, including some devil idea of using the light :twisted:
Should have find some time to update the index again.

Regards,
Desmanto

Re: Either RegExp is wrong or "matches=" is misunderstood

Posted: 08 Nov 2017 21:29
by husky
Desmanto,

I hope your work will bring you joy.

Best mind is a busy mind.







Regards

Husky