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.
    thanks
    Robert
    Robert, Apr 19, 2004
    #1
    1. Advertising

  2. Robert wrote:
    > 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


    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..

    > but am having an amazing amount of trouble -


    What did you try? Show us your code.

    > perhaps it's too late.
    > but if anyone can point me in theright direction, I'd be most
    > gratefull.


    From "perldoc perlre":
    \D Match a non-digit character

    jue
    Jürgen Exner, Apr 19, 2004
    #2
    1. Advertising

  3. Robert

    Vetle Roeim 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.


    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
    string.

    HTH.

    --
    #!/usr/bin/vr
    Vetle Roeim, Apr 19, 2004
    #3
  4. Robert

    Vetle Roeim Guest

    * Vetle Roeim
    > *
    >> 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.

    >
    > Try this regular expression: s/[^\d]//g. I.e.:
    >
    > my $str = '+01-876 003456';
    > $str =~ s/[^\d]//g;


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


    [...]
    --
    #!/usr/bin/vr
    Vetle Roeim, Apr 19, 2004
    #4
  5. Robert

    Vetle Roeim Guest

    * Bernard El-Hagin
    > Vetle Roeim <> wrote:
    >
    >> *
    >>> 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.

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

    > ^^^^^^
    >
    >
    > Don't you think that could be a little bit confusing for a newbie?


    Um, yes. :D The code example should have cleared things up, though.


    --
    #!/usr/bin/vr
    Vetle Roeim, Apr 19, 2004
    #5
  6. Robert

    Web Surfer Guest

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

    In article <40839733$>, says...
    >
    > 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.
    > thanks
    > Robert



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

    Anno Siegel Guest

    Web Surfer <> wrote in comp.lang.perl.misc:
    > [This followup was posted to comp.lang.perl.misc]
    >
    > In article <40839733$>, says...
    > >
    > > 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.
    > > thanks
    > > Robert

    >
    >
    > $string =~ s/\D//g; # remove all non-numeric chars


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

    does the same, but faster.

    Anno
    Anno Siegel, Apr 19, 2004
    #7
  8. Robert

    Vetle Roeim Guest

    * Anno Siegel
    > Web Surfer <> wrote in comp.lang.perl.misc:
    >> [This followup was posted to comp.lang.perl.misc]
    >>
    >> In article <40839733$>, says...
    >> >
    >> > 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.
    >> > thanks
    >> > Robert

    >>
    >>
    >> $string =~ s/\D//g; # remove all non-numeric chars

    >
    > $string =~ tr/0-9//cd;
    >
    > does the same, but faster.


    For anyone interested in _why_ it's faster, this post seems to
    explain it: <URL:http://tinyurl.com/2txz8>


    --
    #!/usr/bin/vr
    Vetle Roeim, Apr 19, 2004
    #8
  9. Robert

    Robert Guest

    Thanks: Re: need Regular Expression to remove all non-numerical

    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



    --
    ---
    Serving The Mission of Our Lady of Fatima.
    At: http://www.the-mission-of-our-lady-of-fatima.org

    "Robert" <> wrote in message news:40839733$...
    >
    > 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.
    > thanks
    > Robert
    >
    >
    Robert, Apr 19, 2004
    #9
  10. -berlin.de (Anno Siegel) wrote:

    > Web Surfer <> wrote:
    >
    > > $string =~ s/\D//g; # remove all non-numeric chars

    >
    > $string =~ tr/0-9//cd;
    >
    > does the same, but faster.


    Since no-one else has posted this improved s///-solution, here
    goes

    s/\D+//g;

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

    Peter

    --
    #!/local/bin/perl5 -wp -*- mode: cperl; coding: iso-8859-1; -*-
    # matlab comment stripper (strips comments from Matlab m-files)
    s/^((?:(?:[])}\w.]'+|[^'%])+|'[^'\n]*(?:''[^'\n]*)*')*).*/$1/x;
    Peter J. Acklam, Apr 20, 2004
    #10
  11. Robert

    Robin Guest

    "Robert" <> wrote in message
    news:40839733$...
    >
    > 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.
    > thanks
    > Robert
    >

    I'd like some of your code... Thanks.
    -Robin
    --
    Robin, Apr 21, 2004
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stitch Jones
    Replies:
    4
    Views:
    576
    Stitch Jones
    Aug 22, 2004
  2. VSK
    Replies:
    2
    Views:
    2,290
  3. Martin
    Replies:
    0
    Views:
    691
    Martin
    May 30, 2005
  4. =?utf-8?B?4LC24LGN4LCw4LGA4LCo4LC/4LC14LC+4LC4?=

    Need a Regular expression to remove a char for Unicode text

    =?utf-8?B?4LC24LGN4LCw4LGA4LCo4LC/4LC14LC+4LC4?=, Oct 13, 2006, in forum: Python
    Replies:
    4
    Views:
    1,502
    Leo Kislov
    Oct 13, 2006
  5. rajesh
    Replies:
    0
    Views:
    178
    rajesh
    Nov 23, 2005
Loading...

Share This Page