fork download
  1. import re
  2. file = "Dear John Buy 1 of A1~A10, cost 10 dollars\n Ivan Buy 20 of Milk\nDear Tina Buy 10 of Coke, cost 100 dollars\n Mary Buy 5 of Milk"
  3. for line in file.split("\n"):
  4. match = re.search(r'\s+(?P<name>\w+)\D*(?P<num>\d+)\sof\s(?P<item>[^,]+)\D*(?P<costs>\d*)',line)
  5. if match:
  6. print(match.groups())
  7.  
Success #stdin #stdout 0.02s 9016KB
stdin
Standard input is empty
stdout
('John', '1', 'A1~A10', '10')
('Ivan', '20', 'Milk', '')
('Tina', '10', 'Coke', '100')
('Mary', '5', 'Milk', '')