Both E4X and Regular Expressions are powerful on their own, but by combining them you can really do some amazing stuff - especially in relation to data filtering...
Manish Jethani outlined data filtering with E4X, but I just wanted to expand on that and show a technique I've been using that combines E4X Predicate Filtering with Regular Expressions.
Consider the following code snippet:
// Create an XML object that a list of employees. For the sake
// of this example, each employee only includes a last name.
var employees:XML =
Schall
Smith
Schaffer
;
// Find all employees with last names beginning with "Sch"
var filtered:XMLList = employees.employee.( /^Sch.*/.test( last ) );
// Outputs:
//
// Schall
//
//
// Schaffer
//
trace( filtered );
In the above example, the regular expression ^Sch.* is used to match strings that begin with "Sch". The employees.employee E4X expression generates a list of all of the employee nodes, and then predicate filtering is used to loop over all of those nodes and pull out that ones that match the filtering expression. The filtering expression is the regular expression tested against the value of the <last> node. When the test() method returns true, the <employee> node is added to the list of filtered nodes because the last name matches against the regex.
You can see that the filtered list of employees contains only the ones with last names of "Schall" and "Schaffer", both of which start with "Sch". The list does not include "Smith" because it does not match the regular expression.
This is an extremely powerful technique for data filtering. I love that regular expression support is finally native to the Flash Player, and by combining this with E4X you can do some amazing things pretty easily.
Tags: E4X, ActionScript, Flex 2, ECMAScript, RegEx, Regular Expressions
5 Comments
Hi Darron !
This blog entry reminds me of a question I wanted to ask but have forgotten until now:
If I query a database via SQL, is it possible, too, to use full-blown regexes ?
If it is not, then Your method of combining search in XML data with regexes seems to me
much more powerful. Then it could make sense to first pull a whole dataset from the
database via SQL, convert the dataset to XML and then search within it using regexes.
What's Your experience/knowledge/opinion with regard to this topic ?
Servus und Tschuess
Kai
Posted by: Kai Tischler | February 27, 2006 10:54 AM
There is no generic statement to answer you question. You can place the filtering code on either the client or the server, and it really depends on what the application is doing to know where the code goes.
If you're doing a lot of data filtering, it might make sense to move the filtering to the database so that the client does not get bogged down with data processing. However, for the simple example that I demonstrated in this post, it doesn't make sense to have the server filter the data since the client can handle it just fine.
RegEx is not natively supported in SQL itself but it can be added to different databases. I've never used a RegEx in SQL before, but if you search for "sql regex" on google there's a lot of results. :-)
Posted by: darron | February 27, 2006 11:32 AM
It looks to me like the regular expression is part of the language. While useful (for type-checking) it seems to limit it's usefulness. Predicate filtering is often done as a result of user interaction, so the input is variable.
So I guess what I'm saying is, can it be a string?
Posted by: Scott Hyndman | February 28, 2006 10:59 AM
Yes, you can create a RegExp out of a string with the RegExp constructor.
var re:RegExp = new RegExp( "^Sch.*" );
var filtered:XMLList = employees.employee.( re.test( last ) );
Posted by: darron | February 28, 2006 11:18 AM
Ah, very cool then.
Posted by: Scott Hyndman | February 28, 2006 11:53 AM