Question

Here i am trying to extract phone no. from emails

                        </tr>
                        <tr>
                            <td height=3D"20" valign=3D"bottom" style=3D"font-s=
    ize:10px; color:#817f80;padding:0px;margin:0px;">1-877-435-2623 | Mon-Fri 8=
    am-5pm, Sat-Sun 8am-12pm PST</td>
                        </tr>
                        <tr>
                            <td height=3D"35" valign=3D"bottom" style=3D"font-s=
    ize:10px; margin:0px; margin-top: 10px; padding:0px; padding-bottom:0px; co=
    lor:#939393;">&copy; 2012 Elance, Inc. |  441 Logue Ave. Suite 150 | Mounta=
    in View, CA 94043</td>
                        </tr>
                        <tr>
                            <td height=3D"20" valign=3D"bottom" style=3D"font-s=
    ize:10px; margin:0px; margin-bottom:25px; padding:0px; color:#939393;"><a h=
    ref=3D"https://www.elance.com/q/legal"><span style=3D"color:#939393;text-de=
    coration:none;">Terms of Service</span></a> | <a href=3D"https://www.elance=
    .com/p/legal/privacy-policy.pdf"><span style=3D"color:#939393;text-decorati=
    on:none;">Privacy Policy</span></a> | All Rights Reserved.</td>
                        </tr>
                    </table>
                    <div style=3D"margin: 0; padding: 0;">
                        <a href=3D"http://www.facebook.com/elance"><img src=3D"=
    https://www.elance.com/images/4.0/referral/facebook-btn-16x16.png" border=
    =3D"0" alt=3D"Facebook" title=3D"Facebook" /></a>&nbsp;
                        <a href=3D"http://www.twitter.com/elance"><img src=3D"h=
    ttps://www.elance.com/images/4.0/referral/twitter-btn-16x16.png" border=3D"=
    0" alt=3D"Twitter" title=3D"Twitter" /></a>&nbsp;
                        <a href=3D"http://www.linkedin.com/company/elance"><img=
     src=3D"https://www.elance.com/images/4.0/nav/nav_logo_sprite/icon-li.gif" =
    border=3D"0" alt=3D"LinkedIn" title=3D"LinkedIn" /></a>&nbsp;
                        <a href=3D"http://www.youtube.com/elance"><img src=3D"h=
    ttps://www.elance.com/images/4.0/nav/nav_logo_sprite/icon-yt.gif" border=3D=
    "0" alt=3D"Youtube" title=3D"Youtube" /></a>
                    </div>
                </td>0345-5407008
345-5124587
124-541-2215
+92-3455412412
3251452114
009124125413
hello world its my no. 03005134012
            </tr>

            </tbody>
        </table>

    </DIV>

    </BODY>
    </HTML>




    --PHP-alt-3fbbc9acbecd103566642de6249238ea--

    --PHP-mixed-3fbbc9acbecd103566642de6249238ea--


    251254125

using regex

 ?(\d|\+)(\+|\d|\(|\)|-| |\.){7,25} ?

but i want to ignore matches found in these strings

--PHP-alt-3fbbc9acbecd103566642de6249238ea--

--PHP-mixed-3fbbc9acbecd103566642de6249238ea--

Try http://rubular.com/r/b6K0LOcn3N Any better regular expression ???
Explanation to my RegEx: ?(\d|\+)(\+|\d|\(|\)|-| |\.){7,25} ?

  1. start catching no. when a space or not a space is detected only.
  2. start matching if first is 0-9 or + match any character in (+|\d|(|)|-| |.)
  3. 7,25 because mini length of phone no. is 7 and
  4. max around 25 including whitespaces and - etc
Was it helpful?

Solution

Try using some word boundaries \b:

(?:\b\d|\+)(?:\+|\d|\(|\)|-| |\.){7,25}\b

And I turned the capture groups into non-capturing groups. If you only need the whole number, I don't think that you need capture groups.

\b matches when there is a 'word' character followed by a 'non-word' character or vice versa (i.e. either \w\W or \W\w).

regex101 demo.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top