Home > front end >  Extracting data from list with RegExp OR in Python3
Extracting data from list with RegExp OR in Python3

Time:11-02

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*|amount\b)", 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!

CodePudding user response:

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']
  • Related