Hello htmanning,

If the phone numbers are punctuated with spaces rather than hyphens, you will need a different strategy. Here is one such approach (which builds on aaron_baugher’s solution):

#! perl use strict; use warnings; use Data::Dump; while (<DATA>) { chomp; # Trim trailing newline my @apts = /[- \d]+/g; # Get all sequences of digits, spaces, & h +yphens s/^[ ]+ //x for @apts; # Trim initial spaces s/ [ ]+$//x for @apts; # Trim trailing spaces @apts = grep { /^\d{3,4}$/ } @apts; # Get 3- or 4-digit sequen +ces only dd \@apts; } __DATA__ John Smith lives in Apts. 123 & 456, home number: 555-6666-7777 Please phone Jane Doe on 111 2222 3333. She lives in apartment 789 in +the Main building. Please phone Janet Roe on 88899990123. She lives in apartment 100 in t +he Main building.

Output:

12:51 >perl 1211_SoPW.pl [123, 456] [789] [100] 12:51 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: Pull 3-digit and 4-digit numbers from string by Athanasius
in thread Pull 3-digit and 4-digit numbers from string by htmanning

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.