[fpc-pascal] My favourite missing feature

S. Fisher expandafter at yahoo.com
Wed Dec 24 10:42:17 CET 2008


--- On Mon, 12/22/08, Mark Morgan Lloyd <markMLl.fpc-pascal at telemetry.co.uk> wrote:

> 
> There's been a recent thread in fpc-other on second
> languages, but it appeared to focus more on what was a
> useful part of a developer's skillset rather than what
> people miss from Pascal.
> 
> What /I/ miss is Perl's pattern matching, and I miss it
> to the extent that in some of my own scripting stuff
> I've implemented it myself:
> 
> IF cells[2, dateTime] =
> /(\d\d)\/(\d\d)\/((\d\d)?\d\d)\s.*/i
> THEN BEGIN
>   cells[6, 1]:= /1/ + ordinalSuffix(/1/);
>   cells[7, 1]:= monthName(/2/);
>   IF /3/ > 999 THEN
>     cells[8, 1]:= /3/
>   ELSE
>     cells[8, 1]:= '20' + /3/
> END;


That regular expression can be made less terrifying to tyros
by using extended mode in Ruby.  We can include whitespace
and even comments.


match_data =
"a day to remember: 31/08/2008   ".match(
  # Start the regex with %r{ so we can use / without escaping it.
  %r{
      # The date begins with 2 digits for the day;
      # enclose them in () to capture them for later use.
      (\d\d)
      # A separator.      
      /
      # The month is 2 digits or numerals.
      (\d\d)
      # The separator.
      /
      (
        # The year can be 4 digits or 2 digits.
        # Now the parentheses are for grouping;
        # we don't care whether they capture or not. 
        (\d\d) ?  # The question mark means preceding item is optional.
        \d\d
      )
      # Our dates must always be followed by whitespace.
      # It can be a space, tab, carriage-return, or linefeed.
      \s
    }x  # End of the regex. The x calls for extended mode.
)

# Our captures are strings; convert them to integers and assign
# them to our variables.
day,month,year = match_data.captures.map{|s| s.to_i }
# Print results.
p day, month, year


To use grep, sed, awk, or a text editor effectively, one must
understand regular expressions.  Anyone who doesn't, isn't a
power user.  Even some users of Mickeysoft Word use regular
expressions for searching.  Ask yourself this: do you want to be
less computer-literate than users of Mickey's software?

To refuse to learn regular expressions because they are cryptic
before you learn them is like refusing to learn English because
it is cryptic before you learn it.



      



More information about the fpc-pascal mailing list