Regular Expressions Help

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
dickey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 118
Joined: Wed Apr 08, 2009 11:54 pm

Regular Expressions Help

Post by dickey » Tue Sep 22, 2009 9:08 am

Hello all,

I have decided to port a web 2.0 application to Revolution.

The existing app has a backend written in PHP, and uses many regular expressions to detect pattern matches particularly in addressing data.

Example PHP code (without wishing anyone to slide into depression).

Code: Select all

$subject = "Westfield Shopping Town, Shop 1400-1402 Kingsway Miranda NSW 2228";
$pattern = '[Shop [0-9]+[-][0-9]+]';
preg_match($pattern, $subject, $matches);

$subject2 = "Westfield Shopping Town, Shops 1400-1402 Kingsway Miranda NSW 2228";
$pattern2 = '[Shops [0-9]+[-][0-9]+]';
preg_match($pattern2, $subject2, $matches2);

$subject3 = "Westfield Shopping Town, Shop 1400/140 Kingsway Miranda NSW 2228";
$pattern3 = '[Shop [0-9]+[/]]';
preg_match($pattern3, $subject3, $matches3);

print_r($matches); 
// results in
 Array ( [0] => Shop 1400-1402 )
print_r($matches2);
// results in
  Array ( [0] => Shops 1400-1402 )
print_r($matches3);
// results in
  Array ( [0] => Shop 1400/ )
So I wish to implement similar regular expression checks in Revolution.

I understand from the respective guides for PHP and Rev that RegExp are based on PCRE - all good. It might then be reasonable to expect the reg exp syntax will not differ greatly then (here's hoping).

I want to retrieve the matching text (if any) like in my PHP examples above. I am using matchText with a variable pre-defined so I can capture the FoundTextVarsList.

So my example Rev code may be:

Code: Select all

   put "Westfield Shopping Town, Shops 1400-1402 Kingsway Miranda NSW 2228" into varTarget
   put empty into varMatchingText
   matchText(varTarget,"([Shop [0-9]+[-][0-9]+])",varMatchingText)
   put varMatchingText into field "dinosaur"
That code bombs with a "handler can't find handler" message, so I am unsure whether I have coded correctly for the retrieval of the text result or that the reg exp is poorly formed for Revolution.

Any assistance greatly appreciated,

Kind regards, Andrew

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Post by Klaus » Tue Sep 22, 2009 9:20 am

Hi Andrew,

I am a complete NOOB when it comes to regex, but "matchtext" is a FUNCTION which returns true or false!


Best

Klaus

dickey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 118
Joined: Wed Apr 08, 2009 11:54 pm

Post by dickey » Tue Sep 22, 2009 9:40 am

Thanks Klaus, You are correct in that the result is equal to true when you leave off the foundTextVarsList parameter.

According to the docs if your define a variable to hold the foundTextVarsList, before you execute matchText, then that variable should hold the matching text for most simple regular expressions.
Syntax from dictionary:
matchText(string,regularExpression[,foundTextVarsList])

The optional foundTextVarsList consists of one or more names of existing variables, separated by commas.

and further...

If the regularExpression includes a pair of parentheses, the substring matching the part of the regular expression inside the parentheses is placed in the first variable in the foundTextVarsList. Additional substrings, matching additional parenthetical expressions within the regularExpression, are placed in additional variables in the foundTextVarsList. The number of parenthetical expressions in the regularExpression should match the number of variables in the foundTextVarsList.
So I was hoping that when you define a variable to hold the foundTextVarsList, whilst the expression may result in true or false, when true it also places the found text in the variable designed to hold foundVarsTextList.

Kind regards, Andrew

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Tue Sep 22, 2009 9:57 am

As Klaus says (and although the docs imply that matchText is more like a command) you need to get RunRev to return a boolean value from matchText by invoking it as a function.
The placeholder variable (list) is populated with the matches, but the returned value from matchText is still a boolean.

So (bearing in mind that your test above looks for "Shop" not "Shops" and that the "sets" cannot be nested) the following code should work:

Code: Select all

put "Westfield Shopping Town, Shops 1400-1402 Kingsway Miranda NSW 2228" into varTarget 
   put empty into varMatchingText 
   get matchText(varTarget,"(Shops [0-9]+[-][0-9]+)",varMatchingText)
   --you can check "if it is true then [match found action] else [no match found action]" too
   --or something like
   --put matchText(varTarget,"(Shops [0-9]+[-][0-9]+)",varMatchingText) into theBooleanValue
   put varMatchingText into field "dinosaur" 
HTH,
SparkOut

dickey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 118
Joined: Wed Apr 08, 2009 11:54 pm

Regular Expressions Help

Post by dickey » Tue Sep 22, 2009 10:24 am

Thanks SparkOut (& Klaus),

Working well now.

Changes:
1. fixed the Shop for Shops typo in the example,
2. added get to the front of the matchText line,
3. removed the opening and closing [ ] from the ported regular expression statement.

Having just tried a variety of regular expressions from my PHP code, it is a welcome relief I won't have to rewrite the regex itself.

Thanks again.

Kind regards,

Andrew

Post Reply