[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] how can I parse out fax numbers from ad texts?



At 11:42 +1000 on 14/8/97, Joseph\"ALVIS\"Alexander Snow wrote:
>eg. ad reads: send letter to oweo;fjh f;jldf, or fax:233-645-56434,
>could be send fax to (232)834-9957, etc...
>you get the picture...

Not knowing what the conventions in the US are, my guess would be something
like this:

  #!perl -w
  use strict;

  my $text = "Fax me on 233-645-56434";
  my( $substring, $number );
  ($substring = $text) =~ m/\({0,1}(\d+)\){0,1}-{0,1}(\d+)-{0,1}(\d+)/si;

  $number = $1.$2.$3;

  print $text, "\n", $number, "\n";

Which provides the output:

  Fax me on 233-645-56434
  23364556434

(That code compiles, and works, not that it's important)

This regular expression will extract anything that looks like

  2-2-2
  (1)2-3
  (1)-2-3

Oops... it'll even do
  (1-2-3
  1)2-3

Or other strange combinations... but it won't handle
 - spaces in the number,
 - more than three groups of digits
 - less than three groups of digits

And even worse, it will match dates that are formatted like 14-8-1997.

You could try replacing each of the -{0,1} groups with something like [-
]{0,1}, which would then allow numbers to be formatted as (1) 2 3 or (1)
2-3, etc. Then try using \d\d\d instead of \d+ so that you don't
accidentally match days or months in dd-mm-yyyy type dates.

For example:
  my $text = "Fax me on (233) 645-56434";
  ($substring = $text) =~
      m/\({0,1}(\d\d\d)\){0,1}[- ]{0,1}(\d\d\d)[- ]{0,1}(\d+)/si;

Which has the same result as the first snippet. This regex is getting large
and bulky - I'm willing to bet most people will be thinking "LTS" by now...
(if not "he's full of it").

Regards,
<Alex>

Windows 95: n. 32 bit extensions and a graphical shell for a 16 bit patch
to an 8 bit operating system originally coded for a 4 bit microprocessor,
written by a 2 bit company.



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch