current stable: 0.99.6 unstable: cvs (0.99.7) |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Regular ExpressionsRegular expressions provide a very powerful method of matching, modifying and extracting information from strings. Using special syntax, code that would usually require line after line of special matching code can be summarised within a one line regular expression (from here on in referred to as a regex). They can either be found within the language, e.g. Perl or ferite, or as an add in library, e.g. Python, php and C. ferite's regex's are providied by means of PCRE (Perl Compatible Regular Expressions, a C library that can be found at http://www.pcre.org) and as a result are almost identical in operation to Perl's. Regex's look like this: Example:
This one will match all occurrences of the string "123" and swap them with "456"
This is more complicated and will match occurrences of "World" and swap them with "Chorlrisl". The reason being is due to back ticks which are discussed soon. There are three types of regular expression support and that is match, swap and capture. They are used as follows:
To match an m is used, to swap an s is used. It is possible to capture strings within the regular expression using the same method as in Perl i.e. By using brackets. The captured strings upon each match are placed into r<bracket number> - this is equivalent to the $1, $2, ... $n strings in Perl. The maximum number of captured strings is currently 99, and example of captured strings is in the above expressions, i.e. (2) would cause "2" to be place within r1, in the second expression (or(l)) would cause "orl" to be placed within r1 and "l" to be placed within r2. OptionsThere are a number of options that can be used to modify the method that the regular expression's execution and processing, these are:
BackticksBackticks are used within the swap mode of the regular expressions. It allows you to used captured strings within string that should replace the matched expression. There are used within the second example above. They are used as follows: a '\' (back slash) followed by the number that you want to use. This is only a brief insight into regular expressions, and a suggested read is "Mastering Regular Expressions" by Jeffrey E. F. Friedl (published by O'Reilly), and that will tell you everything you need to know about regular expressions. :-) It is also suggested that the libpcre documentation is worth reading on http://www.pcre.org.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
ferite et al © 2000-2004, Chris Ross |