JSON patterns

Every programmer knows regular expressions, and how useful they are for matching simple or complex patterns within text (that is, within strings). MyPatterns implement a generalized form of pattern matching, for any data structures, beyond solely strings. This is achieved by using "JSON patterns", an integration of two well-known notations: JSON and regular expressions.

In myPatterns, any standard regular expression (as defined in Java or JavaScript, respectively), enclosed between slashes, can be used to match a string. For example, the pattern /bla/ matches any string containing the substring "bla", at any position; the pattern /^(bla *){3,}/ matches any string starting with at least three "bla", separated by some whitespace, etc.

For matching any other kinds of data (than strings), myPatterns also uses a standard notation familiar to many programmers: JSON (JavaScript Object Notation), rather than defining a whole new notation. JSON is classically used only for data interchange (serializing data to, and deserializing data from, a bit string), but myPatterns slightly extends it for matching (i.e. selecting and decomposing) data, in the following ways:

By nesting several patterns, one can build complex queries. For instance, the pattern [{size:/^(X*L)$/,price:%p}|%t] matches a list starting with an article of size L or XL or XXL, etc., and saves: the article size (because of the matching group in the regex) as metavariable 0 (zero), the article price as metavariable p, and the list of subsequent articles as metavariable t.
Last updated on: 25/6/2010 Contact us