Taming Regular Expressions |
Written by Nikos Vaggalis | |||
Friday, 16 September 2016 | |||
Page 2 of 2
Simple Regex Language, although it embraces similar principles with the GP approach, targets the very language in which we write regular expressions. An example will work best in demonstrating its concept.The following expression matches an email address of the "you@example.com" format, written in the traditional form looks like the output of text generated by the process of obfuscation:
(?:@)(?:[0-9]|[a-z]|[\.-])+(?:\.)[a-z]{2,}$/i
Under SRL the same is naturally expressed as:
once or more, literally "@",
any of (digit, letter, one of ".-") once or more,
literally ".",
letter at least 2 times, must end, case insensitive This example demonstrates that in effect SRL is a Domain Specific Language, that acts as a targeted solution to the domain's problem. This is nothing new, and is frequently used in domains ranging from web development, e.g. the Perl Dancer framework hosting its own DSL, to databases, e.g. SQL which takes the pain out of working with RDBMS's, and language extension DSLs, e.g. Linq and jOOQ which emulate SQL in C# and Java respectively by allowing the writing of SQL statements as if the programming language natively supported them: C#-Linq:
Java-jOOQ:
With SLR, each part of a regular expression part becomes self-explanatory, as we go from this: [a-f]{4} And from this: Or even from this: Putting all together, our very first regex example that matches UK postcodes:
can be now rewritten as:
optional whitespace exactly 1 time letter from D to H,literally "J", letter from W to Z)
Note that we haven't used any capturing parentheses as we are only interested in the concept of proof, that is working out a match rather than working on extracting text. Testing it against some standard postcode samples: M1 1AA validates the correctness of the generated regex. The limits How to use
More Information Related ArticlesAutomatically Generating Regular Expressions with Genetic Programming Advanced Perl Regular Expressions - The Pattern Code Expression Advanced Perl Regular Expressions - Extended Constructs
To be informed about new articles on I Programmer, sign up for our weekly newsletter,subscribe to the RSS feed and follow us on, Twitter, Facebook, Google+ or Linkedin.
Comments
or email your comment to: comments@i-programmer.info
|
|||
Last Updated ( Friday, 16 September 2016 ) |