Python Regex Search string for all substrings having one or more numbers -
in python, have following string:
"fish 14~ reel 14 rod b14"
i want use regex run through loop , return location of each substring having 1 or more numbers in it. ex.:
(): print location of substring
my expect output be:
5 14 21
please help, thanks.
answer: ok tested of following , work. 1 fastest? drum roll.... in order fastest slowest: 1) perreal - 9.7ms 2) jon - 10.5ms 3) m.buettner - 12.3ms 4) upasana - 25.6ms
thanks of python geniuses. there solution didn't test it. various other reasons chose jon's method program.
something like:
s = "fish 14~ reel 14 rod b14" import re words = re.finditer('\s+', s) has_digits = re.compile(r'\d').search print [word.start() word in words if has_digits(word.group())] # [5, 14, 21]
so, find indices of starts of words, check each word see if it's got digits in it...
if indeed last entry should 22 instead of 21, have answer in possible duplicate...
Comments
Post a Comment