| |||||||||||
Regular Expressions in different languages Posted by admin (Graham Ellis), 2 August 2002 * Regular expressions in JavaIn the early days of Java, a regular expression engine was written by Johnathan Locke, and donated to the Apache Software Foundation; you can still download it from http://jakarta.apache.org/regexp/index.html. As of release 1.4 of Java (Spring 2002), though, there's a standard package - java.util.regex - that's shipped with the JRE (Java runtime environment). These new Java regular expressions are described as being very "Perl like", though some of the methods provided for matching (such as lookingAt) will suprise you when they seem to assume a begin anchore by default! * Regular expressions in Perl Regular expressions are fundamental to Perl. The regular expression handler is rich in facilities, many of which are accessed through terse sequences which make Perl regular expressions powerful, short, and hard for the novice to understand. In recent releases such as Perl 5.8, enhancements have been made to give yet better facilities and to support some of the more verbose syntaxes in use in other regular expression handlers. In Perl 6, there will be major changes to regular expressions to enable them for an even bigger future. * Regular expressions in Tcl Tcl supports both a "glob style" pattern matching - as you would use in file name matching in operating system commands - and regular expressions. Up to and including release 8.0 of Tcl, the regular expressions handler offered only the more fundamental facilities, but as from Tcl 8.1, "advanced regular expressions" were added - these give further capabilities, and also allow provide somewhat cleaner syntax for commonly used groups. Since Tcl is an interpretive language that makes its own use of the [ and ] charcaters (amongst others), Tcl regular expressions tend to be grouped in curly braces { and }, or be loaded with backslash protections that make them look daunting. * Regular Expressions in MySQL MySQL provides pattern matching (in commands such as SELECT) through the LIKE, which uses % to match any sequence of characters, and _ to match any one character. It also provides REGEXP (and the synonym RLIKE) which is a regular expression match, which provides a good set of the more fundamental facilities of regular expressions. Note that if you want to explicitly match a special character such as a * in an REGEXP clause, you need to specify \\* since one of the backslashes is stripped off as the query is parsed, before it ever gets to the regular expression handler. * Regular expressions in PHP PHP inlcudes not one but two regular expression handlers! The ereg function (and others with similar names) use POSIX style regular expressions, with character groups such as [[:alnum:]] for an alphanumeric character, and the preg functions use Perl style regular expressions - for example, \w is used for an alphanumic character. Although there's more typing involved, we suggest that newcomers use the POSIX regular expressions for code that they're writing - the resultant regular expressions are much easier to read, and therefore to maintain. * Regular expressions in Ruby Ruby regular expressions are literals written between slashes, with modifiers appended after the second slash. They resemble Perl rather than POSIX style regular expressions (thus \w for an alphanumeric character). Regexp objects can be constructed (remember - Ruby is an OO language!) and used through a variety of methods such as =~ and match. Methods on a MatchData object can be used to retreive values from a match. * Other uses of regular expressions Early (but still current) uses of regular expressions include the grep ("Global Regular Expression Processor") command of Unix, Linux and Mac OS-X operating systems, and its extended cousin egrep that supports further special charcater sequences. The awk pattern scanning and processing language also has its roots in Unix and relies on regular expressions for many of its facilities. Even editors such as sed (the Stream Editor), vi and emacs let you search for text that matches a regular expression! Posted by admin (Graham Ellis), 4 October 2002 Just a note for those of you who haven's seen it - our latest "Of Course" open source newsletter includes a major article on regular expressions, and examples of how to recognise everything from a social security number to a postcode and a bank sort code (all UK style). If you would like me to post you a copy (we post free of charge to any address in the Birtish Isles), please email me your postal address via graham@wellho.netPosted by admin (Graham Ellis), 7 October 2002 Following up myself again I've put some quick reference tables for regular expressions in different languages up at http://www.regularexpression.info. I've tried to help show the "wood" for the "trees", but do let me know if there are any glaring ommissions! This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |