need Regular Expression to remove all non-numerical

R

Robert

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
 
J

Jürgen Exner

Robert said:
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
 
V

Vetle Roeim

* (e-mail address removed)
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.
 
V

Vetle Roeim

* Vetle Roeim
* (e-mail address removed)
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.


[...]
 
V

Vetle Roeim

* Bernard El-Hagin
Vetle Roeim said:
* (e-mail address removed)
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.
 
W

Web Surfer

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

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
 
A

Anno Siegel

Web Surfer said:
[This followup was posted to comp.lang.perl.misc]

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
 
V

Vetle Roeim

* Anno Siegel
Web Surfer said:
[This followup was posted to comp.lang.perl.misc]

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

Robert

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
 
P

Peter J. Acklam

$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
 
R

Robin

Robert said:
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
--
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top