How to extract data from list with RegExp OR in Python3?

Question:

I have the following list

['Amount', 'amount', 'Line Total', 'lineTotal', 'AMOUNT DUE']

and I’m trying to extract ‘Amount’, ‘amount’, ‘Line Total’, ‘lineTotal’ but not ‘AMOUNT DUE’ field from that list.
Here is how I do that:

>>> lst = ['Amount', 'amount', 'Line Total', 'lineTotal', 'AMOUNT DUE']
>>> import re
>>> r = re.compile("(line*|amountb)", re.IGNORECASE)

I’m using regexp OR expression because in real use case I can have different values (e.g. in one of them can be only ‘Amount’ value and smth else, in the other one – ‘Line Total’ etc) and I want to extract exactly that one value depending on what I have.
So in example above I expect to get

['Amount', 'amount', 'Line Total', 'lineTotal']

But instead I’m getting

>>> list(filter(r.match, lst))
['Line Total', 'lineTotal']

Why is it happaning like so I how could I fix that? Thank you in advance!

Answers:

Try (regex101):

import re

lst = ["Amount", "amount", "Line Total", "lineTotal", "AMOUNT DUE"]
r = re.compile(r"(?=line|amount$).*", flags=re.IGNORECASE)

lst = list(filter(r.match, lst))
print(lst)

Prints:

['Amount', 'amount', 'Line Total', 'lineTotal']
Answered By: Andrej Kesely
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.