简体   繁体   中英

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

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

在此处输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM