[Tutor] Testing a string to see if it contains a substring (dw)

dw bw_dw at fastmail.fm
Thu Jan 22 16:40:08 CET 2015


Thanks for your good comments.
I do think I found a work around

body = ""
for x in range(0,len(line_array)):
    test_line = len(re.findall(r'[0-9]{2}/[0-9]{2}/[0-9]{4}',
    line_array[x]))
    if test_line > 0:
        body = body + line_array[x]+"\n"

For each iteration re.findall returns a list object based upon the regex
search.
If an element contains "01/04/2013".....the regex is found, and
re.findall returns it as a list object ['01/04/2013']
I can then perform a len function on that which will = 1, since the list
contains only one string object.
For the row in which the regex is not found, re.findall will return []
Which is essentially a list with 0 objects, and the len function will
then = 0.

Thanks Alan G for your example!!
It will be fun to try it!! :-]
I look for the KISS model in coding, and I like your example.
-- 
 Bw_dw at fastmail.net



More information about the Tutor mailing list