Python quesiton
Edward Glowacki
glowack2@msu.edu
Fri, 2 Feb 2001 10:09:55 -0500 (EST)
On Fri, 2 Feb 2001, Mark Szidik wrote:
> Is there a simple way to test a string to see if it holds numeric
> characters in python? I can't seem to find anything simple.
*whips out the sledgehammer that is Python and prepares to hammer
out some code...*
-------------------------------------------
#!/usr/local/bin/python
#import the regular expression library
import re
# Compile our regular expression if we're going to use it a lot
has_a_number = re.compile(".*\d.*")
def match_numbers(string):
if has_a_number.search(string) == None:
print("No digits")
else:
print("Has digits")
-------------------------------------------
I wrapped it in a function, but really all you need is to test the
return value of has_a_number.search(string), if it's None then the
match failed and there aren't any numbers, otherwise it found a
digit.
--
Edward Glowacki glowack2@msu.edu
Computer Laboratory, Technical Support Services
Michigan State University