I have a list of words, with patterns either “term” or “termNUM”, e.g. “term” or “term02”.
I want to save all terms that ends with digit but remove the ones are purely alphabets.
I am totally new to regex, I tried few options, and get the following:
new_list = [x for x in old_list if re.match("[(^a-zA-Z_)\d]", x)]
It is not working, I know it only need a small tweak somewhere, but with my limited skill in regex, cannot do it quickly.
Tips are highly appreciated.
That’s any character (
.), any number of times (*) followed by a digit (\d). Your list comprehension is correct.You could also do
assuming the empty string is not in the list. I’d prefer this option, as it’s more explicit as to what it does.