Friday, 3 June 2011

Python program which finds the numbers in a given string using regular expression

hi,

Today i was seeing regular expression in python. I just read some regexp funtions so thought to write a program which gives a list of numbers from a given string.

the code is given below...

#! usr/bin/env python
import re
string1 = raw_input("enter the text from which you wanna get only numbers: ")
numlist = re.compile('\d+').findall(string1)
print numlist

i/p:

enter the text from which you wanna get only numbers : hello123 rte34d 5b6b7b 12.03

o/p:

[ '123', '34', '5', '6', '7', '12', '03' ]

Note: it does not support floating point numbers eg:'12.01' is taken as '12','01'

No comments:

Post a Comment