Detecting numbers in a string

value = 'ad.41.bd' if len(value) == len(value.strip({0,1,2,3,4,5,6,7,8,9})): # no numbers else: # numbers present 

Is there a cleaner way to detect numbers in a string in Python?

+7
source share
7 answers

How about this?

 import re if not re.search('\d+', value): # no numbers else: # numbers present 
+12
source
 >>> value="ab3asdf" >>> any(c.isdigit() for c in value) True >>> value="asf" >>> any(c.isdigit() for c in value) False >>> value = 'ad.41.bd' >>> any(map(lambda c:c.isdigit(),value)) True 

EDIT:

 >>> value="1"+"a"*10**6 >>> any(map(lambda c:c.isdigit(),value)) True >>> from itertools import imap >>> any(imap(lambda c:c.isdigit(),value)) True 

the card took 1 second (on the old python) imap was instant, because imap returns the generator. note that in the real world there is a high probability that the number will be at the end of the file name.

+8
source
 from string import digits def containsnumbers(value): return any(char in digits for char in value) 

EDIT:

And only for thoroughness:

any (c.isdigit ()):

 >>> timeit.timeit('any(c.isdigit() for c in value)', setup='value = "abcd1"') 1.4080650806427002 

any (c in numbers):

 >>> timeit.timeit('any(c in digits for c in value)', setup='from string import digits; value = "abcd1"') 1.392179012298584 

re.search (1 or more digits):

 >>> timeit.timeit("re.search('\d+', value)", setup='import re; value = "abcd1"') 1.8129329681396484 

re.search (stop after one digit):

 >>> timeit.timeit("re.search('\d', value)", setup='import re; value = "abcd1"') 1.599431037902832 

re.match (not greedy):

 >>> timeit.timeit("re.match(r'^.*?\d', value)", setup='import re; value = "abcd1"') 1.6654980182647705 

re.match (greedy):

 >>> timeit.timeit("re.match(r'^.*\d', value)", setup='import re; value = "abcd1"') 1.5637178421020508 

any (card ()):

 >>> timeit.timeit("any(map(lambda c:c.isdigit(),value))", setup='value = "abcd1"') 1.9165890216827393 

any (IMAP ()):

 >>> timeit.timeit("any(imap(lambda c:c.isdigit(),value))", setup='from itertools import imap;value = "abcd1"') 1.370448112487793 

As a rule, less complex regular expressions ran faster. c.isdigit() and c in digits almost equivalent. re.match slightly faster than re.search . map() is the slowest solution, but imap() was the fastest (but with a rounding error of any(c.isdigit) and any(c in digits) .

+4
source

You can use regex :

 import re # or if re.search(r'\d', value): if re.match(r'^.*?\d', value): # numbers present else: # no numbers 
+3
source
 if not any(c.isdigit() for c in value) # no numbers else: # numbers present 
+1
source

If you want to know how big the difference is, you can use re.sub ()

 import re digits_num = len(value) - len(re.sub(r'\d','',value)) if not digits_num: #without numbers else: #with numbers - or elif digist_num == 3 
0
source

To detect signs in numbers, use the ? .

 import re if not re.search('-?\d+', value): # no numbers else: # numbers present 
0
source

All Articles