[Tutor] Testing a string to see if it contains a substring
Alan Gauld
alan.gauld at btinternet.com
Thu Jan 22 00:39:38 CET 2015
On 21/01/15 18:14, dw wrote:
> - line_array[1] may contain "01/04/2013 10:43 AM 17,410,217
> DEV-ALL-01-04-13.rlc\n"
> - line_array[2] may contain "01/25/2013 03:21 PM 17,431,230
> DEV-ALL-01-25-2013.rlc\n"
> - line_array[3] may contain "\n"
>
> I want to retain all elements which are valid (i.e. contains a date
> value xx/xx/xxxx)
You need a bit more detail.
Can a row contain anything other than just \n if there is no date?
Is the date always at the start and always the same length?
> So I'm using a regex search for the date value located at the start of
> each element...this way
>
> misc_int1 =
> re.search(r'[0-9]{2}/[0-9]{2}/[0-9]{4}',line_array[x]).start()
You might be better with re.match() if its always at the start of the line.
> This method works really well when the regex date characters are found.
> It returns the value 0 to misc_int1 because the date regex starts at 0
> position.
> I was hoping the .start() method would return a -1 value for the
> instance in which the regex search string is not found.
You can simply split the code over two lines and check the result:
result = re.search(r'[0-9]{2}/[0-9]{2}/[0-9]{4}',line_array[x])
if result is not None:
misc_intl = result.start()
There are no prizes for writing code on one line, so don't be afraid
to separate it out.
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list