h2non / jsonpath-ng

Finally, a JSONPath implementation for Python that aims to be standard compliant. That's all. Enjoy!
Apache License 2.0
572 stars 85 forks source link

Regex case insensitive #105

Closed LongBeachHXC closed 1 year ago

LongBeachHXC commented 2 years ago

I have a scenario where I am trying to match on a user's email. The object will look like so:

{ 
    "result": [
        {
            "email": "Jon.Doe@google.com",
            "attr2": "attr2 value",
            "attr3": "attr3 value",
        }
    ]
}

And I'm trying to match with the following expression:

search_expression = parse('$.result[?(@.email =~ "jon.doe" )]')

It does not find the user object.

So, then I tried something like this and it still didn't work:

search_expression = parse('$.result[?(@.email =~ "/jon.doe/i" )]')

Is there a way to force the regex for case insensitive?

1wang-wood commented 1 year ago

""" use jsonpath_ng.ext """ import jsonpath_ng.ext.filter as jsonpath_filter jsonpath_filter.OPERATOR_MAP.update({'=~': lambda a, b: True if re.search(b, a, re.IGNORECASE) else False}) """ your json path code """

dennis-sayed commented 1 year ago

@LongBeachHXC i tested now this and work fine, but if we want to reset after standard behaviours there is a way? I think it is jsonpath_filter.OPERATOR_MAP.update({'=~': lambda a, b: True if re.search(b, a, 0) else False}) right?

michaelmior commented 1 year ago

@dennis-sayed That would indeed be the correct way to restore standard behavior (although you can leave off 0 as the final parameter since it's the default). Closing since this seems to resolved, but feel free to reopen if you're still having an issue.