Search code examples
pythonpython-3.xlistpython-re

How to set end of string in Python regex?


I have the following list

lst = ['BILL_FROM:', 'MyCompany', '525._S._Lexington_Ave.', 'Burlington._NC._2725', 'United_States', 'musicjohnliofficial@gmail.com', 'BILL_TO:', 'O.Relly', '343._S._Lexington_Ave.', 'Burlington._NC._2725', 'United_States', 'musicjohnliofficial@gmail.com', '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


Solution

  • 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)