简体   繁体   English

正则表达式匹配所有内容,直到其中包含异常的模式

[英]Regex to match everything until pattern with an exception in it

This is my regex这是我的正则表达式

\d+%[^\.][^0-9]*?((?!original).)percentage*

And I want it to match from a percentage (ie 10%) until the word percentage我希望它从一个百分比(即 10%)匹配到单词百分比

  • 10% "whatever" percentage 10%“随便”百分比

except if it contains the word "original":除非它包含“原始”一词:

  • 10% original percentage 10% 原始百分比

So, "whatever" can be anything until the word "percentage" except if he word "original" is in it.因此,“无论”在“百分比”一词之前可以是任何东西,除非他在其中包含“原始”一词。

I've been able to get my regex but it only works correctly if "percentage" is at the beggining of the new line我已经能够得到我的正则表达式,但它只有在“百分比”位于新行的开头时才能正常工作

In some cases, 10% of the sales starts with the original percentage --> my regex match with this string but I don't want to because it contains the word "original"在某些情况下,10% 的销售额以原始百分比开头 --> 我的正则表达式与此字符串匹配,但我不想这样做,因为它包含单词“original”

The 10% of the sales starts with a certain percentage --> my regex match with this string, it's okay because it doesn't containt the word "original" 10% 的销售额以一定的百分比开始 --> 我的正则表达式匹配这个字符串,没关系,因为它不包含“原始”这个词

The 10% of the original原来的10%
percentage of the sale is higher--> my regex doesn't match with this string, and it's okay because it containts the word "original" (maybe because the new line starts with percentage?)销售百分比更高-->我的正则表达式与此字符串不匹配,这没关系,因为它包含单词“original”(可能是因为新行以百分比开头?)

The 10% of the original sale原销售额的 10%
is the percentage of that --> my regex match with this string but I don't want to because it contains the word "original"是那个百分比 - >我的正则表达式与这个字符串匹配,但我不想因为它包含单词“original”

I'm sorry if my explanation is a little weird, English is not my first language.如果我的解释有点奇怪,我很抱歉,英语不是我的第一语言。

Thanks!!!谢谢!!!

You have to repeat this part ((?.original).) and omit the * after percentage* as it optionally repeats the e char.您必须重复这部分((?.original).)并省略* after percent percentage*因为它可以选择重复e字符。

Then if you don't want to match digits in between, you can match any char except a newline or a digit using [^\d\r\n] instead of the .然后,如果您不想匹配两者之间的数字,则可以使用[^\d\r\n]而不是.

\d+%[^.](?:(?!original\b)[^\d\r\n])*\bpercentage\b

The pattern matches:模式匹配:

  • \d+% Match 1+ digits and % \d+%匹配 1+ 个数字和%
  • [^.] Match any char except a dot (Note that this is a broad match, you might also use a space instead) [^.]匹配除点以外的任何字符(注意这是广泛匹配,您也可以使用空格代替)
  • (?: Non capture group (?:非捕获组
    • (?!original\b)[^\d\r\n] Match any char except a digit or newline when wat is directly to the right is not original (?!original\b)[^\d\r\n]当 wat 直接位于右侧时,匹配除数字或换行符之外的任何字符不是original字符
  • )* Close the group and repeat it 0+ times )*关闭组并重复 0+ 次
  • \bpercentage\b Match percentage \bpercentage\b匹配percentage

Regex demo正则表达式演示

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

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