need Regular Expression to remove all non-numerical

Discussion in 'Perl Misc' started by Robert, Apr 19, 2004.

  1. Robert

    Robert Guest

    I'm trying to come up with a regex that will remove all non-numeric characters from a string.
    ie: +01-876 003456

    to: 01876003456

    but am having an amazing amount of trouble - perhaps it's too late.
    but if anyone can point me in theright direction, I'd be most gratefull.
    Robert, Apr 19, 2004
    1. Advertisements

  2. Please note, regular expressions don't "remove" anything.
    Regular expressions "match" text, .... and then maybe the s() operator
    replaces the matched text with some other text or the m() operator returns
    true, etc..
    What did you try? Show us your code.
    From "perldoc perlre":
    \D Match a non-digit character

    Jürgen Exner, Apr 19, 2004
    1. Advertisements

  3. Robert

    Vetle Roeim Guest

    Try this regular expression: s/[^\d]//g. I.e.:

    my $str = '+01-876 003456';
    $str =~ s/[^\d]//g;

    [^\d] means match all characters that are _not_ digits, and the /g
    specifies that the substitution should continue throughout the

    Vetle Roeim, Apr 19, 2004
  4. Robert

    Vetle Roeim Guest

    * Vetle Roeim
    Eh... That could of course be "s/\D//g" instead.

    Vetle Roeim, Apr 19, 2004
  5. Robert

    Vetle Roeim Guest

    * Bernard El-Hagin
    Um, yes. :D The code example should have cleared things up, though.
    Vetle Roeim, Apr 19, 2004
  6. Robert

    Web Surfer Guest

    [This followup was posted to comp.lang.perl.misc]

    $string =~ s/\D//g; # remove all non-numeric chars
    Web Surfer, Apr 19, 2004
  7. Robert

    Anno Siegel Guest

    $string =~ tr/0-9//cd;

    does the same, but faster.

    Anno Siegel, Apr 19, 2004
  8. Robert

    Vetle Roeim Guest

    * Anno Siegel
    For anyone interested in _why_ it's faster, this post seems to
    explain it: <URL:>
    Vetle Roeim, Apr 19, 2004
  9. Robert

    Robert Guest

    Thanks folks,
    I was completly off the track - I was doing a search for the specific characters,
    $word = "+1-767-123456";
    $word =~ s/\+- //g;
    print "Word: $word\n";

    but all this returns is the same string.
    I was simply not understanding how the matching and substitution was supposed to work I think.

    Now, thanks, I have three other methods to use that DO work.
    $word = "+1-767-123456";
    $word =~ tr/0-9//cd;
    print "Word: $word\n";

    $word = "+1-767-123456";
    $word =~ s/[^\d]//g;
    print "Word: $word\n";

    $word = "+1-767-123456";
    $word =~ s/[\D]//g;
    print "Word: $word\n";
    Word: 1767123456
    Word: 1767123456
    Word: 1767123456
    Word: +1-767-123456
    Robert, Apr 19, 2004
  10. Since no-one else has posted this improved s///-solution, here


    but I am sure tr/// will be faster.

    Peter J. Acklam, Apr 20, 2004
  11. Robert

    Robin Guest

    I'd like some of your code... Thanks.
    Robin, Apr 21, 2004
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.