Platalea Minor
Platalea Minor

Reputation: 877

how to match exact word in regex

I have a string below, I would like to extract number (12,000) from TOTAL but not from CLIENT SUB-TOTAL

CLIENT SUB-TOTAL :       12,000                                    12,000
                                                          ------------  ------------  ------------  ------------
                                                  TOTAL :       12,000                                    12,000

I have tried with the following regex, but it still matches 2 group.

(?:TOTAL :       )([0-9]+[,]+[0-9]+)

Thank you for your help

Upvotes: 1

Views: 968

Answers (2)

The fourth bird
The fourth bird

Reputation: 163447

You don't need the non capture group (?: in the pattern that you tried as you are matching the text only.

Also not that you don't need the square brackets here [,]+ and due to the + it could possibly also match ,,,,

What you might do is assert a whitespace boundary to the left if a negative lookbehind is supported, and instead of hardcoding the exact amount of spaces, match 1 or more whitespace chars without a newline using [^\S\r\n]+

(?<!\S)TOTAL[^\S\r\n]+:[^\S\r\n]+([0-9]+,[0-9]+)\b

Regex demo

Upvotes: 1

David542
David542

Reputation: 110342

You could try doing a negative assertion that TOTAL isn't preceded by a dash. Something like the following would work:

enter image description here

Upvotes: 1

Related Questions