Can't using regexp

Post your questions and help other users.

Moderator: Martin

clever_man
Posts: 9
Joined: 15 Sep 2015 11:43

Can't using regexp

Post by clever_man » 15 Sep 2015 11:54

Hi!
Can't using regexp.
Trying example code from documentation, but it doesn't work:

Code: Select all

result = matches("1234567", "\\d{3}.*");
log(result);
In logs result get false ((
Trying more simple:

Code: Select all

result = matches("1234567", "1");
log(result);
But result false too.
Whats wrong?

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Can't using regexp

Post by Martin » 15 Sep 2015 13:06

Hi,

Automagic replaces variables within curly braces in strings (check section String Inline Expressions in the documentation of action Script).
In your case "\\d{3}.*" will actually be processed to "\\d3.*". You can use single quotes to avoid that {3} is replaced by 3: '\\d{3}.*'

Pattern 1 does not match 1234567 since the pattern needs to match the entire string. You could extend the pattern to result = matches("1234567", "1.*");.

Regards,
Martin

clever_man
Posts: 9
Joined: 15 Sep 2015 11:43

Re: Can't using regexp

Post by clever_man » 15 Sep 2015 13:21

But I wan't to find exact simbol '1' and "1.*" find the whole string "1234567".
Where I can test rexep? I trying on http://regexpal.com/ but it not situable for Automagic regexp...
Can I'm use regexp for text file?

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Can't using regexp

Post by Martin » 15 Sep 2015 13:50

Automagic uses the regex-support of Android/Java. A tester like http://java-regex-tester.appspot.com/ should work.
Adding a simple tester directly within Automagic would be useful. I'll add it to the todo-list.

You can use capturing groups to extract text:

Code: Select all

value = "my code is 1234";
list = newList();
result = matches(value, '.*(\\d{4}).*', list);
log(result);// true

log(list);// [my code is 1234, 1234]
The first entry of the list contains the entire matched string, the value at index 1 contains the text matched by the first capturing group in parantheses.

clever_man
Posts: 9
Joined: 15 Sep 2015 11:43

Re: Can't using regexp

Post by clever_man » 15 Sep 2015 14:36

Trying simple example.
By Init Var Tex File read in resTest this text:

Code: Select all

<HTML><HEAD><TITLE> Web Authentication Redirect</TITLE><META http-equiv="Cache-control" content="no-cache"><META http-equiv="Pragma" content="no-cache"><META http-equiv="Expires" content="-1"><META http-equiv="refresh" content="1; URL=https://login.wi-fi.ru/am/UI/Login?org=mac&service=coa&client_mac=4c-3c-16-19-ed-fb&ForceAuth=true"></HEAD></HTML>

Then trying next script:

Code: Select all

ls = newList();
log(resTest);
lines = split(resTest, "\\r?\\n");
for (line in lines)
{
   log(line);
   tmp = matches(line, "a.*", ls);
   log(tmp);
}
log(ls);
But it false.

Log:

Code: Select all

[My Flow] Start executing action 'Script: ls = newList(); //resTest = "abc1\nabc2\nabc3"; log(resTest); lines = split(resTest, "\\r?\\n"); for (line in lines) { log(line); tmp = matches(line, "a.*", ls); log(tmp); } log(ls);'
[My Flow] <HTML><HEAD><TITLE> Web Authentication Redirect</TITLE><META http-equiv="Cache-control" content="no-cache"><META http-equiv="Pragma" content="no-cache"><META http-equiv="Expires" content="-1"><META http-equiv="refresh" content="1; URL=https://login.wi-fi.ru/am/UI/Login?org=mac&service=coa&client_mac=4c-3c-16-19-ed-fb&ForceAuth=true"></HEAD></HTML>
[My Flow] 
[My Flow] <HTML><HEAD><TITLE> Web Authentication Redirect</TITLE><META http-equiv="Cache-control" content="no-cache"><META http-equiv="Pragma" content="no-cache"><META http-equiv="Expires" content="-1"><META http-equiv="refresh" content="1; URL=https://login.wi-fi.ru/am/UI/Login?org=mac&service=coa&client_mac=4c-3c-16-19-ed-fb&ForceAuth=true"></HEAD></HTML>
[My Flow] false
[My Flow] []
[My Flow] End executing action 'Script: ls = newList(); //resTest = "abc1\nabc2\nabc3"; log(resTest); lines = split(resTest, "\\r?\\n"); for (line in lines) { log(line); tmp = matches(line, "a.*", ls); log(tmp); } log(ls);'
[My Flow] Flow ended.
Why?

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Can't using regexp

Post by Martin » 17 Sep 2015 19:02

It seems that there's no line of text that starts with a. matches checks whether the whole input text matches the pattern, not just partially.

clever_man
Posts: 9
Joined: 15 Sep 2015 11:43

Re: Can't using regexp

Post by clever_man » 17 Sep 2015 19:09

Martin wrote:It seems that there's no line of text that starts with a. matches checks whether the whole input text matches the pattern, not just partially.
But ".*a.*" doesn't work too.
How I can find and using url for this text?

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Can't using regexp

Post by Martin » 17 Sep 2015 19:51

The next EAP version will include a simple regular expression tester which should help to work with regular expressions.

An expression like this could work (not tested with your input):

Code: Select all

(?s).*URL=(.*)".*
(?s) turns on DOTALL mode so that a dot also matches newlines.
The url can be found with getElement(groups, 1), when the input matches.

I'm not a regular expression expert so there might be much better ways to do it. I often also use the functions indexOf and substring to extract some text.

clever_man
Posts: 9
Joined: 15 Sep 2015 11:43

Re: Can't using regexp

Post by clever_man » 18 Sep 2015 07:10

Martin wrote:The url can be found with getElement(groups, 1), when the input matches.
Intersting situation - in matches() for regexp I'm using ' ' instead " " (because " using in line and the expression cause error with " ").
After this, when I'm trying getElement() all script cause error in in the log after getElement(); append a symbol '. I think that is bug.
Screenshot_2015-09-18-10-06-24.png
Screenshot_2015-09-18-10-06-24.png (259.12 KiB) Viewed 15591 times

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Can't using regexp

Post by Martin » 21 Sep 2015 12:56

The dialog that displays the error shows the entire script within single quotes. The marked single quote corresponds to the single quote tight before ls=newList.
The real cause of the problem can probably be identified in the log. What does the log show after executing this script (menu->Log)?

Post Reply