This seems like it might a good fit to regular expressions but lets first talk about how to restrict the search space so you don't have to use regex. Before you even start though you need to make sure your requirements are possible - it looks like you want to match 16 to storage and 12 to screen size without requiring GB or inches at the end, which means you need to find some way to distinguish those numbers. Here's a few ideas of how to make these restrictions:
Storage: this is very likely to be powers of 2, (8, 16, 32, 64...) so you only really need to check if the string has a power of two in it:
[storage in "iPhone 5s 16 GB" for storage in ["8", "16", "32", "64"]]
will return you [False, True, False, False]
, telling you that there's a 16 in there.
Display: again, these are likely to be quite restricted - as you say, 12, 13, etc. You could probably get away with something similar to my storage trick above and ignore trying to find inches.
Brand, Model: These should be really simple - again, make a list of the brands you're searching for, and then do the list comprehension above.
If this doesn't work, you can try regexes. These are more complex and usually more trouble than they're worth - they're hard to read, maintain and I've often got bitten by them. However:
The regex (?i)[0-9]+(\s|)GB
in python will do a case insensitive ((?i)
) search for strings containing one of more numbers 0-9 ([0-9]+
), zero or more white space (\s*
) and then the letters GB (GB
). GB will be matched in upper or lower case because of the (?i)
option are the start. Calling
re.search('(?i)[0-9]+\s*GB', "Apple iPhone 5s (Space Grey, 16GB)")
will return a match object giving you the matched string.
I find regexs quite complicated so general use a helpful website to at least try catch bugs before I code them.