Search code examples
pythonregexbooleanany

Wildcard Match Between Characters


I'd like to have bool if a wildcard search is true. Specifically, if the following exist: _<any number or char>_.

Ideally I'd like to find an elegant pythonic way. For example, any returns true for exact matches. Is there an equivalent for wildcard?

>>> lst = ['cherry', 'pineapple_1', 'apple', '_olive', 'banana_split_1']
>>> any('_.' in elem for elem in lst)
False
>>> any('_' in elem for elem in lst)
True
>>> any('_*_' in elem for elem in lst)
False

To clarify, the second command should return True b/c of 'banana_split_1' element containing a characters between two underscores.


Solution

  • You can always use str.find(), it would probably be the fastest for such a simple pattern, too:

    >>> lst = ['cherry', 'pineapple_1', 'apple', '_olive', 'banana_split_1']
    >>> any(x.find('_', x.find('_') + 2) != -1 for x in lst)
    True
    

    EDIT - Explanation: It loops through every element of the lst and attempts to find an underscore that is followed by another underscore as long as there is at least one character between them. Consider unraveling it for a single case:

    >>> test = 'banana_split_1'
    >>> index = test.find('_')  # 6, the index of the first underscore
    >>> index2 = test.find('_', index + 2)  # 12, the index of the next underscore
    >>> index2 != -1
    True
    

    If there wasn't a second underscore (removed by 2 spaces to the right so it requires at least one character between) the index2 would be -1 and the test in the above generator would fail.

    This is, obviously, repeated for all entries until a match is found or any() returns False.