I have the following list
lst = ['BILL_FROM:', 'MyCompany', '525._S._Lexington_Ave.', 'Burlington._NC._2725', 'United_States', '[email protected]', 'BILL_TO:', 'O.Relly', '343._S._Lexington_Ave.', 'Burlington._NC._2725', 'United_States', '[email protected]', 'INVOICE_number', '01', 'INVOICE_DATE', '2022-12-27', 'AMOUNT_DUE', '1.128', 'SUBTOTAL', '999.00', 'TAX_(13.0%)', '129.87', 'TOTAL', '1.128']
And I want to get it's BILL_TO:
field using regex.
I'm trying to do
>>> bill_to = re.compile("(\w )to$", re.IGNORECASE)
>>> list(filter(bill_to.match, lst))
to get ['BILL_TO:']
field only, but instead getting
['Burlington._NC._2725', 'BILL_TO:', 'Burlington._NC._2725', 'SUBTOTAL']
Why the $
symbol is not working here? Or am I doing something else wrong?
Thank you
CodePudding user response:
The $
will match the end of the string, but you have a :
beforehand which you also need to match:
(\w )to:$
Also, it's recommended to use a raw string to escape the \
(notice the r
):
bill_to = re.compile(r"(\w )to:$", re.IGNORECASE)