Ella has asked for the wisdom of the Perl Monks concerning the following question:

I need to search various texts in Italian. These texts may be produced by any number of unknown different authors using slightly different conventions. I need to pull out as many of the telephone or fax numbers as possible with as low a rate of false positives as possible (I'd rather pick out 1 of 10 numbers accurately than get 11 out of 10 if you follow). The format of the numbers is variable. Unlike the north american (000) 000-0000 format, telephone and fax numbers in Italy can be anywhere from 3 to 15+ digits in length. Numbers can include .'s, spaces, /'s, -'s and ()'s and may be preceded by a space or a +

here are *some* examples of commonly used telephone number formats with their contexts:

poliambulatorio 050/701901
Tel. +39 0422 519111
Fax +39 0422 969501
Phone: +39.087105891
Fax: +39.0871562496
(+39) 06 6783933
(+39) 06 678793
Tel: 0668136061
Phone 39-0445-575870 Fax 39-0445-575399
P.I. 02212120246
Telefono 091.545606

these are the constraints that I have come up with so far, the problem is that it's a little too constraining and I'm not getting any results at all.

m/ ( numer ( i (tel|utili|verdi|facsimili|fax) |o (verde|tel|facsimile|fax|tel) ) |t: |f: |fax |al\snumero |phone |p.i. ) [^\d]{,7} (\d\s*){3,15} ([^\d]|$) /gx
Thanks for your help. Thanks especially to LAI for helping me make this question a tad less silly.

'share and enjoy'

Replies are listed 'Best First'.
Re: regexp quandry
by japhy (Canon) on Jun 19, 2003 at 22:47 UTC
    First of all, you can't leave out the lower bound to a quantifier. [^\d]{,7} is trying to match a non-digit, and then the text "{,7}". I suggest using \D{0,7}. (In fact, use \D instead of [^\d] in general.)

    The regex I'd use would probably look like this:

    m{ ($prefix) \D{0,7} (\d\s*){3,15} (?!\d) }gx
    where $prefix is some sub-pattern that matches likely leading text.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: regexp quandry
by LAI (Hermit) on Jun 20, 2003 at 13:25 UTC

    A couple of things I missed while helping you out earlier:

    Instead of:

    |p.i.

    it should be

    |p\.i\.

    Otherwise, each period will match any character instead of a literal period.

    Also, the modifiers at the end, instead of /gx, should be /gxi to enable case-insensitive matching.

    LAI

    __END__
Re: regexp quandry
by Ella (Acolyte) on Jun 27, 2003 at 00:09 UTC

    yes, anonymous monk, to clarify LAI's point, I need to be able to do this because I'm helping to make a voice synthesizer and in human speech telephone numbers are usually realized differently from e.g. an integer, especially in many romance languages like italian, which is what I'm working on. I don't have any intention of calling anyone with this information, nor am I trying to extract phone numbers with the aim of calling these people up.

    'share and enjoy'

A reply falls below the community's threshold of quality. You may see it by logging in.