@@ -58,7 +58,7 @@ class JSONPath:
5858 # operators
5959 REP_SLICE_CONTENT = re .compile (r"^(-?\d*)?:(-?\d*)?(:-?\d*)?$" )
6060 REP_SELECT_CONTENT = re .compile (r"^([\w.']+)(, ?[\w.']+)+$" )
61- REP_FILTER_CONTENT = re .compile (r"@([.\[].*?)(?=<=|>=|==|!=|>|<| in| not| is)|len\(@([.\[].*?)\)" )
61+ REP_FILTER_CONTENT = re .compile (r"@([.\[].*?)(?=<=|>=|==|!=|>|<| in| not| is|\s|\)|$ )|len\(@([.\[].*?)\)" )
6262
6363 # annotations
6464 f : list
@@ -216,7 +216,7 @@ def key_func(t, k):
216216 def _filter (self , obj , i : int , path : str , step : str ):
217217 r = False
218218 try :
219- r = self .eval_func (step , None , {"__obj" : obj })
219+ r = self .eval_func (step , None , {"__obj" : obj , "RegexPattern" : RegexPattern })
220220 except Exception :
221221 pass
222222 if r :
@@ -293,6 +293,10 @@ def _trace(self, obj, i: int, path):
293293 if step .startswith ("?(" ) and step .endswith (")" ):
294294 step = step [2 :- 1 ]
295295 step = JSONPath .REP_FILTER_CONTENT .sub (self ._gen_obj , step )
296+
297+ if "=~" in step :
298+ step = re .sub (r"=~\s*/(.*?)/" , r"@ RegexPattern(r'\1')" , step )
299+
296300 if isinstance (obj , dict ):
297301 self ._filter (obj , i + 1 , path , step )
298302 self ._traverse (self ._filter , obj , i + 1 , path , step )
@@ -332,6 +336,16 @@ def _trace(self, obj, i: int, path):
332336 return
333337
334338
339+ class RegexPattern :
340+ def __init__ (self , pattern ):
341+ self .pattern = pattern
342+
343+ def __rmatmul__ (self , other ):
344+ if isinstance (other , str ):
345+ return bool (re .search (self .pattern , other ))
346+ return False
347+
348+
335349def compile (expr ):
336350 return JSONPath (expr )
337351
0 commit comments