Writing regex in python where some columns might be missing data

Viewed 40

I am trying to write a regex that will work for for both these cases: The table I am working with is actually on a pdf file but I had the data in excel also.

This is the table I'm working with

This is what I have so far, but it only works when all the columns are populated

([\d+,]+\.\d+ \w+) ([\d+,]+\.\d+) ([\d,]+\.\d+) ([\d,]+\.\d+)? ([\d,]+\.\d+)? (\d+\/\d+ yrs)? (\([\d,]+\.\d+\))? ([\d,]+\.\d+)

I would like my search to return "None" when the columns are not populated. For example, first line in searching would be:

40.01 SQ 90 20.01 650.01 4,750.00 4,750.00

How can I get group(6) and group(7) to be "None"?

Second line would be: 40.01 SQ 76.9 1,200.20 659.22 47,233.12 10/75 yrs (2,444.23) 37,254.22

I've been playing around with optional quantifiers but not having much luck. Any help is greatly appreciated.

1 Answers

There are some misunderstandings and lack of preparation in the regex presented. The following regex would match at least the second row of data.

([\d,]+(?:.\d+)? [A-Za-z]+) ([\d,]+(?:\.\d+)?) ([\d,]+(?:\.\d+)?) ([\d,]+(?:\.\d+)?) ([\d,]+(?:\.\d+)?) (\d+\/\d+ yrs) (-?[\d,]+(?:\.\d+)?) ([\d,]+(?:\.\d+)?)

If the value of the 7th column will be like (2,444.23) instead of -2,444.23 then try folloing.

([\d,]+(?:.\d+) [A-Za-z]+) ([\d,]+(?:\.\d+)?) ([\d,]+(?:\.\d+)?) ([\d,]+(?:\.\d+)?) ([\d,]+(?:\.\d+)?) (\d+\/\d+ yrs) ((?:[\d,]+(?:\.\d+)?)|(?:\([\d,]+(?:\.\d+)?\))) ([\d,]+(?:\.\d+)?)
Related