Question

I would like to emulate asterisk extension matching in python. For example:

3494ZXXXXXX

Would match:

34941000000

But not match:

34940000000

I would like to support any matching operator provided by asterisk. I do not have an asterisk available, so no AMI can be used. So what I need is a way to implement this function:

def asterisk_extension_match(extension, pattern):
    '''Returns True if the extension matches the pattern, 
       according to the asterisk extension matching rules'''
    # Compute matches
    return matches

Is there any library implementing this pattern matching, or do I have to emulate it using regexes or similar? Do I need to reinvent the wheel?

Was it helpful?

Solution

You can accomplish this using regex by replacing xzn.
try some thing like this:

import re
def asterisk_extension_match(extension, pattern):
    '''Returns True if the extension matches the pattern,
       according to the asterisk extension matching rules'''
    pattern = re.sub(r'x', '[0-9]', pattern, flags=re.I)
    pattern = re.sub(r'z', '[1-9]', pattern, flags=re.I)
    pattern = re.sub(r'n', '[2-9]', pattern, flags=re.I)
    match = re.match(pattern, extension)        
    if match:
        return match.group()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top