简体   繁体   中英

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.

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 .

Something like this?

for elem in list:
    if bool(re.search(expression, elem)):
        return True
return False

I think the key item you need is an alphanumeric descriptor, such as _[0-9A-Za-z]+_

0-9  digits
A-Z  upper-case
a-z  lower-case
[]   any one of these characters
+    at least one repetition (disallows null string)

Surround that in underscores, and I believe that's your search pattern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM