By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,809 Members | 1,999 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,809 IT Pros & Developers. It's quick & easy.

how to remove numbers from a string?

P: 30
How to remove all the numbers from a string like this?

$string="dfjgkk4kgl56lfka88lf"

Thanks a lot!
Sep 10 '08 #1
Share this Question
Share on Google+
4 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
Please know that I am not normally one for just giving up code. Its not condusive to your learning. But, this is one of those times where I am inclined to do so.

You would do something like the following which is a substitution, replacing all digits with nothing.

Expand|Select|Wrap|Line Numbers
  1. $string =~ s/\d//g;
  2.  
Untested, but I think it should work.

Regards,

Jeff
Sep 10 '08 #2

P: 30
Please know that I am not normally one for just giving up code. Its not condusive to your learning. But, this is one of those times where I am inclined to do so.

You would do something like the following which is a substitution, replacing all digits with nothing.

Expand|Select|Wrap|Line Numbers
  1. $string =~ s/\d//g;
  2.  
Untested, but I think it should work.

Regards,

Jeff

Thanks for your help! You're right, I will try to think more before my asking.
Thank you again!
Sep 11 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
probably a bit more efficient:

Expand|Select|Wrap|Line Numbers
  1. $string =~ tr/0-9//cd;
c = compliment of search pattern
d = delete

tr/// is very good for these simpler type of task requiring a regexp. s/// is good for the more complex stuff.
Sep 11 '08 #4

P: 30
probably a bit more efficient:

Expand|Select|Wrap|Line Numbers
  1. $string =~ tr/0-9//cd;
c = compliment of search pattern
d = delete

tr/// is very good for these simpler type of task requiring a regexp. s/// is good for the more complex stuff.
That's helpful! Thank you!
Sep 11 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.