2

I have two query strings, both of which contains wildcards. We can check if any of the two query strings are present like below:

import re
def wildcards2regex (pattern):
    return ( pattern.replace('?', '[a-zA-Z]{1}').replace('*', '.*') )

string = 'christopher susan'
s1='chris*'
r1 = wildcards2regex(s1)
s2 = 'sus??'
r2 = wildcards2regex(s2)
q = r1+'|'+r2
bool(re.search(q, string))

Now I wonder what to do if I want to check if both query string are present? obviously replacing '|' with '&' does not work. Do anyone know how to achieve that?

Wiktor Stribiżew
  • 561,645
  • 34
  • 376
  • 476
zesla
  • 9,459
  • 12
  • 63
  • 127

2 Answers2

4

You may consider this code:

>>> import re
>>> def wildcards2regex (pattern):
...    return ( pattern.replace('?', '[a-zA-Z]').replace('*', '.*') )
...
>>> string = 'christopher susan'
>>> s1='chris*'
>>> r1 = wildcards2regex(s1)
>>> s2 = 'sus??'
>>> r2 = wildcards2regex(s2)
>>> q = re.compile(r'(?=.*{})(?=.*{})'.format(r1, r2))
>>> bool(re.search(q, string))
True

Take note of this regex building:

q = re.compile(r'(?=.*{})(?=.*{})'.format(r1, r2))

Here we are building a regex with 2 conditions defined using positive lookahead assertions that asserts both of your query strings.

anubhava
  • 713,503
  • 59
  • 514
  • 593
1

You can combine multiple independently positioned search terms into one regex using positive lookahead, since that doesn't consume characters or advance the conceptual cursor.

^(?=.*term1)(?=.*term2)

Demonstration: https://pythex.org/?regex=%5E(%3F%3D.*term1)(%3F%3D.*term2)&test_string=Here%20are%20term1%20and%20term2.

Patrick Parker
  • 4,633
  • 3
  • 18
  • 47