简体   繁体   English

正则表达式匹配第二个单词或另一个单词的第一个出现

[英]Regex to match second word or the first occurance of the another word

I have a situation where I need to parser the below content 我有一种情况需要分析以下内容

Some Text
Some Text
SUB-TOTAL          :    $0.90 
Some Text          :    $0.79-
SUB-TOTAL          :    $0.11 
TAX                :    $0.05 
TOTAL      $0.16 

I need to parse till last 'SUB-TOTAL' if Tax does not exist, else it has to parse till TAX I am not sure where I did wrong in the regex * . 如果不存在Tax ,我需要解析到最后一个“ SUB-TOTAL”,否则必须解析到TAX我不确定我在正则表达式中错在哪里*。 ?(?=(TAX|(SUB-TOTAL. ?SUB-TOTAL))) * Since have less experience on regex. ?(?=(TAX |( SUB-TOTAL。?SUB-TOTAL)))*由于对正则表达式的经验较少。 Can some one please help me out. 有人可以帮帮我吗。

Thanks in advance. 提前致谢。

To get up-to and including SUB-TOTAL or TAX with its value, you can use: .*(?:(?:SUB-TOTAL|TAX)[^\\n]*) ... Is that the right cut-off? 要了解最新信息并包括SUB-TOTAL或TAX的值,可以使用: .*(?:(?:SUB-TOTAL|TAX)[^\\n]*) ...是否正确?关闭?

EDIT OK - made my head hurt... but to get last SUB-TOTAL if it exists, otherwise TAX: 编辑确定-使我的头部受伤...但是如果存在,请减去最后一个小计,否则征税:

^(?:(?:(?!.*SUB-TOTAL.*).*TAX[^\n]*)|(?:.*SUB-TOTAL[^\n]*))

Hope this works in your regex engine! 希望这在您的正则表达式引擎中起作用!

EDIT2 This version does not return the SUB-TOTAL/TAX line EDIT2此版本不返回SUB-TOTAL / TAX行

^(?:(?:(?!.*SUB-TOTAL.*).*(?=TAX))|(?:.*(?=SUB-TOTAL)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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