I've looked at this answer and this answer to try to figure out my problem, but I'm not sure they're directly applicable because a) I don't have a condition that always has to be met, and b) the document is so messy that allowing for any of the three to match would result in a large amount of false positives.
So, with that being said, here is my issue. I have lines of text that I want to match that look like this:
x = "10/04 Some brief description blah blah blah 45.00"
where the spacing between everything is messy. Then, I have some lines of text that I want to match that look like this:
y = "VJ../VI Another stupid brief description 1000.00"
z = "11/13 This is another description LO05.13"
The regular expression I'm currently using is this:
regex = r"^(\d\d\s?[1/]\s?\d\d\s?[1/]\d\d)\s+(\S+(?:\s+\S+)*?)\s+(-?\s?[\d,]+\.\d\d)"
The problem is that in y
regex
doesn't match because there is no date at the beginning of the string; the OCR process messed up. However, we still know that it's a valid line because it has a description and an amount. regex
won't match z
either because the amount is not a bunch of digits, but we know it's a transaction because there's a date and a description.
I've considered changing the regex to look like this:
regex = r"^(\d\d\s?[1/]\s?\d\d\s?[1/]\d\d\s+)?(\S+(?:\s+\S+)*?)\s+(-?\s?[\d,]+\.\d\d)?"
But I'm worried that that will just match everything in the document (i.e. "Withdrawals and Debits"). And since the two optional pieces of the line of text are on opposite ends of the more consistent piece of the text, I'm not sure how to implement |
like in the solutions to the questions I linked.
Is my best option to just make two different regular expressions, linked with |
, like so?
regex = r"^(\d\d\s?[1/]\s?\d\d\s?[1/]\d\d\s+)?(\S+(?:\s+\S+)*?)\s+(-?\s?[\d,]+\.\d\d)|^(\d\d\s?[1/]\s?\d\d\s?[1/]\d\d)\s+(\S+(?:\s+\S+)*?)\s+(-?\s?[\d,]+\.\d\d)?"
Any assistance would be appreciated. Thanks
r'^(\w+[^\s/]*/\w{2}\b.*?)\s*(\d+\.\d{2})$'
– Wiktor Stribiżew Apr 14 '16 at 14:29x
) and some that don't work with the current regex (y
andz
). You also have the expected output: matchx
,y
, andz
. They're related because they're all lines I want to capture and should all be captured with one regular expression. The description of what I'm doing is aimed to show what I've tried, which is preferred on SO. So... please tell me how your comment is useful? – brittenb Apr 14 '16 at 14:34/
followed with 2 alpha... ends with a float number. Something like that. – Wiktor Stribiżew Apr 14 '16 at 14:53