I am trying to write a regex pattern it fails for 2 data point sampletext = """…
- User
I am trying to write a regex pattern it fails for 2 data point sample_text = """ 1Cetaphil Gentle Cleansing Lotion - Oily Skin 125 ml 415.68 461.86 (-10%)1 BTL415.6874.82 (18%)490.50 2Solasafe sunscreen gel spf 50 480.68 600.85 (-20%)1480.6886.52 (18%)567.20 3IPCA Acne OC Moisturizer Cream - 75 gm 378.98 473.73 (-20%)1378.9868.22 (18%)447.20 """ item_pattern = re.compile( r'(\d+)(.?)\s+([\d.]+)\s\n' # Item number, name, rate r'([\d.]+)\s*(([-\d%]+))\s*' # Taxable value, discount r'(\d+)\s*' # Quantity r'([\d.]+)\s*' # Tax base r'([\d.]+)\s*(([\d%]+))\s*' # Tax amount, tax percentage r'([\d,.]+)' # Total amount, allow comma , re.DOTALL # Make . match newlines ) matches = item_pattern.findall(sample_text)
for match in matches: print("Item Number:", match[0]) print("Item Name:", match[1]) print("Item Rate:", match[2]) print("Taxable Value:", match[3]) print("Discount:", match[4]) print("Quantity and Unit:", match[5]) print("Tax Amount:", match[6]) # Corrected label print("Tax Percentage:", match[7]) print("Total Amount:", match[8]) print("-" * 40)
print("Matches:", matches) , Item Number: 1 Item Name: Cetaphil Gentle Cleansing Lotion - Oily Skin 125 ml 415.68 461.86 (-10%)1 BTL415.6874.82 (18%)490.50 2Solasafe sunscreen gel spf 50 Item Rate: 480.68 Taxable Value: 600.85 Discount: -20% Quantity and Unit: 1480 Tax Amount: .6886.5 Tax Percentage: 2 Total Amount: 18%
Item Number: 3 Item Name: IPCA Acne OC Moisturizer Cream - 75 gm Item Rate: 378.98 Taxable Value: 473.73 Discount: -20% Quantity and Unit: 1378 Tax Amount: .9868.2 Tax Percentage: 2 Total Amount: 18%
Matches: [('1', 'Cetaphil Gentle Cleansing Lotion - Oily \nSkin 125 ml 415.68\n461.86 (-10%)1 BTL415.6874.82 (18%)490.50\n2Solasafe sunscreen gel spf 50', '480.68', '600.85', '-20%', '1480', '.6886.5', '2', '18%', '567.20'), ('3', 'IPCA Acne OC Moisturizer Cream - 75 \ngm', '378.98', '473.73', '-20%', '1378', '.9868.2', '2', '18%', '447.20')]
- Assistant