Skip to content
Advertisement

Regex match specific string without other string

So I’ve made this regex:

JavaScript

to match only the first of the following two sentences:

  1. discount of €50,20 on these items
  2. This item on sale now for €30,20

As you might’ve noticed already, I’d like the amount in the 2nd sentence not to be matched because it’s not the discount amount. But I’m quite unsure how to find this in regex because of all I could find offer options like:

JavaScript

This option, as can be seen in my example, does not seem to be the solution to my issue.

Example: https://www.phpliveregex.com/p/y2D

Suggestions?

Advertisement

Answer

You can use

JavaScript

See the regex demo.

Details

  • (?<!bfors) – a negative lookbehind that fails the match if there is a whole word for and a whitespace immediately before the current position
  • – a euro sign
  • (d+(?:,d+)?) – Group 1: one or more digits followed with an optional sequence of a comma and one or more digits

See the PHP demo:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement