Why can't Regular Expressions use keywords instead of characters?

后端 未结 14 3147
甜味超标
甜味超标 2021-02-20 06:46

Okay, I barely understand RegEx basics, but why couldn\'t they design it to use keywords (like SQL) instead of some cryptic wildcard characters and symbols?

Is it for pe

14条回答
  •  盖世英雄少女心
    2021-02-20 07:08

    Perl 6 is taking a pretty revolutionary step forward in regex readability. Consider an address of the form: 100 E Main St Springfield MA 01234

    Here's a moderately-readable Perl 5 compatible regex to parse that (many corner cases not handled):

     m/
         ([1-9]\d*)\s+
         ((?:N|S|E|W)\s+)?
         (\w+(?:\s+\w+)*)\s+
         (ave|ln|st|rd)\s+
         ([:alpha:]+(?:\s+[:alpha:]+)*)\s+
         ([A-Z]{2})\s+
         (\d{5}(?:-\d{4})?)
      /ix;
    

    This Perl 6 regex has the same behavior:

    grammar USMailAddress {
         rule  TOP {     }
    
         rule  addr { <[1..9]>\d* ?
                        }
         token direction { N | S | E | W }
         token streetname { \w+ [ \s+ \w+ ]* }
         token streettype {:i ave | ln | rd | st }
         token city {  [ \s+  ]* }
         token state { <[A..Z]>**{2} }
         token zip { \d**{5} [ - \d**{4} ]? }
      }
    

    A Perl 6 grammar is a class, and the tokens are all invokable methods. Use it like this:

    if $addr ~~ m/^$/ {
         say "$, $";
    }
    

    This example comes from a talk I presented at the Frozen Perl 2009 workshop. The Rakudo implementation of Perl 6 is complete enough that this example works today.

提交回复
热议问题