Skip to content

Escaping '/' in regex filter selector #124

Closed

Description

@Omniflux

I have not been able to figure out how to properly escape '/' in a regular expression for the regex filter selector.

Not sure if it is not currently possible or if it could just use an example in the documentation...

>>> doc = [{'type': 'studio/material/a'}, {'type': 'studio/material/b'}]

Without '/' (success)

>>> jsonpath.compile('$[?@type =~ /studio.*/]').findall(doc)
[{'type': 'studio/material/a'}, {'type': 'studio/material/b'}]

With '/' (error expected)

>>> jsonpath.compile('$[?@type =~ /studio/material/.*/]').findall(doc)
jsonpath.exceptions.JSONPathSyntaxError: expected TOKEN_COMMA, found 'TOKEN_NAME'
  -> '$[?@type =~ /studio/material/.*/]' 1:22
  |
1 | $[?@type =~ /studio/material/.*/]
  |                       ^^^^^^ expected TOKEN_COMMA, found 'TOKEN_NAME'

With '\/'

>>> jsonpath.compile('$[?@type =~ /studio\/material\/.*/]').findall(doc)
jsonpath.exceptions.JSONPathSyntaxError: unexpected token '\\'
  -> '$[?@type =~ /studio\\/material\\/.*/]' 1:29
  |
1 | $[?@type =~ /studio\/material\/.*/]
  |                              ^ unexpected token '\\'

With '\\/'

>>> jsonpath.compile('$[?@type =~ /studio\\/material\\/.*/]').findall(doc)
jsonpath.exceptions.JSONPathSyntaxError: unexpected token '\\'
  -> '$[?@type =~ /studio\\/material\\/.*/]' 1:29
  |
1 | $[?@type =~ /studio\/material\/.*/]
  |                              ^ unexpected token '\\'

With '\\\/'

>>> jsonpath.compile('$[?@type =~ /studio\\\/material\\\/.*/]').findall(doc)
jsonpath.exceptions.JSONPathSyntaxError: unexpected token '\\'
  -> '$[?@type =~ /studio\\\\/material\\\\/.*/]' 1:30
  |
1 | $[?@type =~ /studio\\/material\\/.*/]
  |                               ^ unexpected token '\\'

Activity

  1. added
    bug
    on Jan 6, 2026
  2. jg-rp commented on Jan 6, 2026

    @jg-rp
    Owner

    Hi @Omniflux,

    This was a bug in the JSONPath parser. Should be fixed in version 2.0.2, now released.

    import jsonpath
    
    query = "$[?@type =~ /studio\\/material\\/.*/]"
    query_raw = r"$[?@type =~ /studio\/material\/.*/]"
    
    data = [
        {"type": "studio/material/a"},
        {"type": "studio/material/b"},
        {"type": "studio foo"},
    ]
    
    
    print(jsonpath.findall(query, data))
    print(jsonpath.findall(query_raw, data))
    
    print(jsonpath.findall("$[?@type =~ /studio.*/]", data))

    Output

    [{'type': 'studio/material/a'}, {'type': 'studio/material/b'}]
    [{'type': 'studio/material/a'}, {'type': 'studio/material/b'}]
    [{'type': 'studio/material/a'}, {'type': 'studio/material/b'}, {'type': 'studio foo'}]
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions