import re
import sublime, sublime_plugin

class TotalFileCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		cleaned = []
		numbers = []

		region = sublime.Region(0, self.view.size());

		for lineRegion in self.view.lines(region):
			line = self.view.substr(lineRegion)

			if (line == ""):
				break

			try:
				m = re.match(ur"£\s*([0-9\.,]{1,9})\s(.*)", line)

				if (m): 
					cost = float(m.group(1).strip(' '))
					numbers.append(cost)
					desc = m.group(2)

					cleaned.append(u"£{0:>9.2f} {1}".format(cost, desc))
			except ValueError:
				cleaned.append(line)

		total = sum(numbers)
		while cleaned[-1].strip() == '':
			del cleaned[-1]

		cleaned.append("")
		cleaned.append(u"£{0:>9.2f} Total".format(total))
		cleaned = '\n'.join(cleaned)

		#edit = self.view.begin_edit("")
		self.view.erase(edit, region)
		self.view.insert(edit, 0, cleaned)
		#self.view.end_edit(edit)


import re
import sublime, sublime_plugin

class TotalFileCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		cleaned = []
		numbers = []

		region = sublime.Region(0, self.view.size());

		for lineRegion in self.view.lines(region):
			line = self.view.substr(lineRegion)

			if (line == ""):
				break

			try:
				m = re.match(u"£\s*([0-9\.,]{1,9})\s*(.*)", line, re.U)

				if (m):
					cost = float(m.group(1).strip(' '))
					numbers.append(cost)
					desc = m.group(2)

					cleaned.append(u"£{0:>9.2f} {1}".format(cost, desc))
				else:
					cleaned.append(line)
			except ValueError:
				cleaned.append(line)

		total = sum(numbers)
		if (len(cleaned) > 0):
			while cleaned[-1].strip() == '':
				del cleaned[-1]

		cleaned.append("")
		cleaned.append(u"£{0:>9.2f} Total".format(total))
		cleaned = '\n'.join(cleaned)

		#edit = self.view.begin_edit("")
		self.view.erase(edit, region)
		self.view.insert(edit, 0, cleaned)
		#self.view.end_edit(edit)


Handle non-matching lines which don't cause match errors and don't clean the empty lines if there are no cleaned lines
Handle non-matching lines which don't cause match errors and don't clean the empty lines if there are no cleaned lines

  import re
  import sublime, sublime_plugin
  
  class TotalFileCommand(sublime_plugin.TextCommand):
  	def run(self, edit):
  		cleaned = []
  		numbers = []
  
  		region = sublime.Region(0, self.view.size());
  
  		for lineRegion in self.view.lines(region):
  			line = self.view.substr(lineRegion)
  
  			if (line == ""):
  				break
  
  			try:
- 				m = re.match(ur"£\s*([0-9\.,]{1,9})\s(.*)", line)
+ 				m = re.match(u"£\s*([0-9\.,]{1,9})\s*(.*)", line, re.U)
  
- 				if (m): 
+ 				if (m):
  					cost = float(m.group(1).strip(' '))
  					numbers.append(cost)
  					desc = m.group(2)
  
  					cleaned.append(u"£{0:>9.2f} {1}".format(cost, desc))
+ 				else:
+ 					cleaned.append(line)
  			except ValueError:
  				cleaned.append(line)
  
  		total = sum(numbers)
+ 		if (len(cleaned) > 0):
- 		while cleaned[-1].strip() == '':
+ 			while cleaned[-1].strip() == '':
- 			del cleaned[-1]
+ 				del cleaned[-1]
  
  		cleaned.append("")
  		cleaned.append(u"£{0:>9.2f} Total".format(total))
  		cleaned = '\n'.join(cleaned)
  
  		#edit = self.view.begin_edit("")
  		self.view.erase(edit, region)
  		self.view.insert(edit, 0, cleaned)
  		#self.view.end_edit(edit)
  
  
Handle non-matching lines which don't cause match errors and don't clean the empty lines if there are no cleaned lines
## Code Before:
import re
import sublime, sublime_plugin

class TotalFileCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		cleaned = []
		numbers = []

		region = sublime.Region(0, self.view.size());

		for lineRegion in self.view.lines(region):
			line = self.view.substr(lineRegion)

			if (line == ""):
				break

			try:
				m = re.match(ur"£\s*([0-9\.,]{1,9})\s(.*)", line)

				if (m): 
					cost = float(m.group(1).strip(' '))
					numbers.append(cost)
					desc = m.group(2)

					cleaned.append(u"£{0:>9.2f} {1}".format(cost, desc))
			except ValueError:
				cleaned.append(line)

		total = sum(numbers)
		while cleaned[-1].strip() == '':
			del cleaned[-1]

		cleaned.append("")
		cleaned.append(u"£{0:>9.2f} Total".format(total))
		cleaned = '\n'.join(cleaned)

		#edit = self.view.begin_edit("")
		self.view.erase(edit, region)
		self.view.insert(edit, 0, cleaned)
		#self.view.end_edit(edit)


## Instruction:
Handle non-matching lines which don't cause match errors and don't clean the empty lines if there are no cleaned lines
## Code After:
import re
import sublime, sublime_plugin

class TotalFileCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		cleaned = []
		numbers = []

		region = sublime.Region(0, self.view.size());

		for lineRegion in self.view.lines(region):
			line = self.view.substr(lineRegion)

			if (line == ""):
				break

			try:
				m = re.match(u"£\s*([0-9\.,]{1,9})\s*(.*)", line, re.U)

				if (m):
					cost = float(m.group(1).strip(' '))
					numbers.append(cost)
					desc = m.group(2)

					cleaned.append(u"£{0:>9.2f} {1}".format(cost, desc))
				else:
					cleaned.append(line)
			except ValueError:
				cleaned.append(line)

		total = sum(numbers)
		if (len(cleaned) > 0):
			while cleaned[-1].strip() == '':
				del cleaned[-1]

		cleaned.append("")
		cleaned.append(u"£{0:>9.2f} Total".format(total))
		cleaned = '\n'.join(cleaned)

		#edit = self.view.begin_edit("")
		self.view.erase(edit, region)
		self.view.insert(edit, 0, cleaned)
		#self.view.end_edit(edit)

