Skip to content
Advertisement

Regex match specific string without other string

So I’ve made this regex:

/(?!for )€([0-9]{0,2}(,)?([0-9]{0,2})?)/

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:

(?!foo|bar)

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

(?<!bfors)€(d+(?:,d+)?)

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:

$strs= ["discount of €50,20 on these items","This item on sale now for €30,20"];
foreach ($strs as $s){
    if (preg_match('~(?<!bfors)€(d+(?:,d+)?)~', $s, $m)) {
        echo $m[1].PHP_EOL;
    } else {
        echo "No match!";
    }
}

Output:

50,20
No match!
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement