简体   繁体   中英

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% "whatever" percentage

except if it contains the word "original":

  • 10% original percentage

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"

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"

The 10% of the original
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?)

The 10% of the original sale
is the percentage of that --> my regex match with this string but I don't want to because it contains the word "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.

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+%[^.](?:(?!original\b)[^\d\r\n])*\bpercentage\b

The pattern matches:

  • \d+% Match 1+ digits and %
  • [^.] 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
  • )* Close the group and repeat it 0+ times
  • \bpercentage\b Match percentage

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