DEV Community

Discussion on: Open Source Adventures: Episode 81: Exploring Raku Regular Expression API

Collapse
 
bbkr profile image
Paweł bbkr Pabian

I have a feeling that you are applying Perl approach to Raku (underscore naming, not being explicit about unicode awareness, escaping instead of quoting constants in regexps).

There is nothing wrong with that - TIMTOWTDI. However grammars are first class citizens in Raku and that allows to write it in normalized manner:

sub parse-date($_) {

    my token year { <[0..9]> ** 4 }
    my token month { <[0..9]> ** 2 }
    my token day { <[0..9]> ** 2 }

    if /
      <year> '-' <month> '-' <day> |
      <year> '/' <month> '/' <day> |
      <day> '/' <month> '/' <year>
    / {
        [+$<year>, +$<month>, +$<day>]
    }
}
Enter fullscreen mode Exit fullscreen mode

It took me a while to unlearn creating bulky Perl regexps. Old habits die hard. Regexp interpolation in Perl was tricky and normalization was often avoided. But it is really worth it as it produces much cleaner and less error prone code in Raku.

BTW: token do not backtrack as opposed to regex.