2

Text:

Total amount due (inc. GST $7.68) $84.55

Regex:

Total Amount.?\s*(?!\(inc\))?[^\$]*(\$\s?[0-9,]+(\.[0-9]{1,})?)

https://regex101.com/r/YXr023/1

but it matches the first amount that it finds $7.68. How do I ignore anything inside brackets so it matches $84.55 instead?

Appending (?![^\(]*\)) to the end ignored the line completely, which I don't want.

eozzy
  • 66,048
  • 104
  • 272
  • 428
  • Is there only one set of brackets possible? Will there always be a number outside (after) the brackets? Does it matter that your question regex and the regex in your link are different? What about https://regex101.com/r/YXr023/3 ? Instead of trying to make it all non-greedy, just greedy match up to the last $ ... ? – TessellatingHeckler Feb 28 '17 at 04:06
  • Yes, only one set of bracket. The description includes ignoring parenthesis part which wasn't working. But yours seems to be working! – eozzy Feb 28 '17 at 04:09
  • @TessellatingHeckler .. except when the amount is a few lines later (bad OCR). Example: http://pastebin.com/ikL3Jvt0 – eozzy Feb 28 '17 at 04:13
  • In other words, You are looking for 2nd occurrence of the desired pattern? Check [Match at every second occurrence](http://stackoverflow.com/questions/589667/match-at-every-second-occurrence) and [How to find 3rd occurrence of a pattern in a line](http://stackoverflow.com/questions/5422949/how-to-find-the-3rd-occurrence-of-a-pattern-on-a-line) – behkod Feb 28 '17 at 04:21
  • @BehradKhodayar Optionally, not always. – eozzy Feb 28 '17 at 04:27
  • It is safe to assume that the target is just the number at the end of the line? i.e., `/Total Amount.*\s\$([\d.]+)/`? – dawg Feb 28 '17 at 05:16
  • @dawg No, thats the complexity. OCR is terrible, the $amount can be several lines below. I've almost achieved it with `Total Amount.*(?![\(]*\))+?[^\$]*(\$\s?[0-9,]+(\.[0-9]{1,})?)(?=\s{2,})` – eozzy Feb 28 '17 at 05:32

1 Answers1

0

Hope this post will help you out..

Regex: (?<!\()\$[\d\.]+(?!.*?\))

1. (?<!\() negative look behind for (

2. \$[\d\.]+ match strings like $10.1000

3. (?!.*?\)) does not contain any further )

Regex Demo

Community
  • 1
  • 1
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
  • Thanks but I'm not extracting $ amounts by itself, I'm checking the label and then looking ahead, so I need to use my regex but just ignore anything inside the brackets – eozzy Feb 28 '17 at 04:00
  • I'm using this regex on several docs and not just this one, so yours fails on every other. Thanks anyway. – eozzy Mar 01 '17 at 00:26