[Vala] Regular expression literals for Vala



Hi,

I have written a patch that adds regular expression literal support for Vala. See more details in

https://bugzilla.gnome.org/show_bug.cgi?id=607702

The following example demonstrates the feature:

-----------

using GLib;


static int main (string[] args)
{
       MatchInfo info;

       // Simple greedy regular expression matching.
       var str1 = "mississippi";
       if (@/(is.*ip)/.match (str1, 0, out info)) {
stdout.printf ("Part of %s is '%s'...\n", str1, info.fetch (1));
       } else {
               stdout.printf ("Did not match at all.\n");
       }

       // Match caseless.
       var str2 = "demonStration";
       if (@/mon(str.*o)n/i.match (str2, 0, out info)) {
stdout.printf ("Part of %s is '%s'...\n", str2, info.fetch (1));
       } else {
               stdout.printf ("%s did not match at all.\n", str2);
       }

       // Match and pick substrings.
       var ts   = "Time: 10:42:12";
       if (@/Time: (..):(..):(..)/.match (ts, 0, out info)) {
stdout.printf ("%s\n\thours = %s\n\tminutes = %s\n\tseconds = %s\n\n", ts, info.fetch (1), info.fetch (2), info.fetch (3));
       }

       // Replace demo: word swapping
       try {
               var str = "apple grape";
stdout.printf ("'%s' becomes '%s'\n", str, @/^([^ ]*) *([^ ]*)/.replace (str, -1, 0, """\2 \1"""));
       } catch (RegexError err) {
               // Replacing still needs exception catching
               message (err.message);
       }

       return 0;
}

----

Any comments welcome.

Regards,
JP

__
Saunalahdelta mullistava uutuus Nokia N97 ja kaiken kattava TotaaliMAX nyt ennakkomyynnissä uskomattoman 
edullisesti! Paketissa puhelut ja viestit (3000 min/kpl/kk), huippunopea mobiililaajakaista sekä Nokia N97 
yhdellä kk-maksulla vain 59,95 e/kk (24 kk sopimus).
Katso http://saunalahti.fi/totaalimax




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]