Flagsmith / flagsmith-engine

This project powers the core Flagsmith API flag evaluations engine.
https://flagsmith.com/
BSD 3-Clause "New" or "Revised" License
10 stars 6 forks source link

REGEX operator only matches characters at the beginning of a string #207

Open rolodato opened 3 weeks ago

rolodato commented 3 weeks ago

REGEX is implemented using Pattern.match, which only checks the beginning of the search string:

If zero or more characters at the beginning of string match this regular expression, return a corresponding Match

This means a regular expression like world will not match a trait value of hello-world.

We should be using Pattern.search instead: https://docs.python.org/3.11/library/re.html#re.Pattern.search

khvn26 commented 3 weeks ago

I'm leaning towards closing this issue as a wontfix:

  1. Such a change would be breaking for all SaaS customers.
  2. re.search is significantly less performant.
  3. A pattern like .*world should cater for the described use case, unless I'm missing something.
rolodato commented 3 weeks ago

I can confirm using a pattern like .*world.* works, thanks for that suggestion!

There is probably a better way than using Pattern.search, it was the first thing that came to mind :)

I would argue this is a bugfix and not a breaking change. We don't document this specifically, but I would say the expectation for "matches regex" is essentially the same way grep behaves. grep world does match hello-world-hello, and does not require adding anything else around world to match.

We have this assumption built-in to every SDK that I've checked so far.

Node.js

> 'hello-world-hello'.match(/world/)
[ 'world', index: 6, input: 'hello-world-hello', groups: undefined ]

Ruby

irb(main):001> 'hello-world-hello'.match?('world')
=> true

Golang

package main

import "regexp"

func main() {
    matched, err := regexp.Match(`world`, []byte(`hello-world-hello`))
    println(matched, err)
    // true, nil
}