Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>>> 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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading