@@ -92,7 +92,20 @@ def matches(self, regex):
9292 :rtype: QueryRegex
9393 """
9494
95- return QueryRegex (self ._key , regex )
95+ return QueryRegex (self ._key , regex , re_method = 'match' )
96+
97+ def search (self , regex ):
98+ """
99+ Run a regex test against a dict value.
100+
101+ >>> where('f1').search(r'\d+')
102+ 'f1' ~= \d+
103+
104+ :param regex: The regular expression to pass to ``re.search``
105+ :rtype: QueryRegex
106+ """
107+
108+ return QueryRegex (self ._key , regex , re_method = 'search' )
96109
97110 def test (self , func ):
98111 """
@@ -411,16 +424,18 @@ def __repr__(self):
411424 return '({0}) and ({1})' .format (self ._cond_1 , self ._cond_2 )
412425
413426
427+
414428class QueryRegex (AndOrMixin ):
415429 """
416430 Run a regex test against a dict value.
417431
418432 See :meth:`.Query.matches`.
419433 """
420434
421- def __init__ (self , key , regex ):
435+ def __init__ (self , key , regex , re_method ):
422436 self .regex = regex
423437 self ._key = key
438+ self .re_method = re_method
424439
425440 def __call__ (self , element ):
426441 """
@@ -429,8 +444,12 @@ def __call__(self, element):
429444
430445 if self ._key not in element :
431446 return False
432-
433- return re .match (self .regex , element [self ._key ])
447+
448+ if self .re_method == 'match' :
449+ return re .match (self .regex , element [self ._key ])
450+
451+ if self .re_method == 'search' :
452+ return re .search (self .regex , element [self ._key ])
434453
435454 def __repr__ (self ):
436455 return '\' {0}\' ~= {1} ' .format (self ._key , self .regex )
@@ -478,7 +497,15 @@ def matches(self, regex):
478497 See :meth:`.Query.matches`.
479498 """
480499
481- self ._special = QueryRegex (self ._key , regex )
500+ self ._special = QueryRegex (self ._key , regex , re_method = 'match' )
501+ return self
502+
503+ def search (self , regex ):
504+ """
505+ See :meth:`.Query.search`.
506+ """
507+
508+ self ._special = QueryRegex (self ._key , regex , re_method = 'search' )
482509 return self
483510
484511 def test (self , func ):
0 commit comments