Skip to content

Instantly share code, notes, and snippets.

@hiteshd
Created May 21, 2015 11:23
Show Gist options
  • Save hiteshd/1482fe9ea22753f99744 to your computer and use it in GitHub Desktop.
Save hiteshd/1482fe9ea22753f99744 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys
import re
master_pattern = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-"
if len(sys.argv) != 2:
print "Usage: ./%s [pattern]\n" % sys.argv[0]
sys.exit(1)
input_pattern = sys.argv[1]
matches = re.match("\[([a-zA-Z0-9_-])\-([a-zA-Z0-9_-])\]", input_pattern)
if matches:
first_element = matches.group(1)
last_element = matches.group(2)
first_element_idx = master_pattern.index(first_element)
last_element_idx = master_pattern.index(last_element)
for index in range(first_element_idx, last_element_idx + 1):
print master_pattern[index]
@andyxning
Copy link

Note: This gist has some bugs in parse the bracket pattern. The recommended way is proposed by swenzel. People can check it out here

---------------------------------------DEPRECATED----------------------------------------------------------

# expand bracket pattern
# we have an assumption that we can have only **a-zA-Z0-9_-** in one bracket.
# no regular modifiers like '*', '+' or '?' appended.
def expand_bracket(query):
    expanded = set()
    start = query.find('[')
    if start == -1:
        expanded.add(query)
        return expanded
    end = query[start:].find(']')
    if end == -1:
        raise BadArgument()
    if '[' in query[start + 1:end]:
        raise BadArgument()
    end = start + end

    alternatives = list(query[start + 1:end].encode('utf8'))
    length = len(alternatives)
    selected = set()
    i = 0
    while i < length:
        alt = alternatives[i]
        if i < (length - 2):
            # like 'ab-'
            if alternatives[i + 1] != '-':
                selected.add(alt)
                i += 1
                continue
            # like 'a-b' or 'a-1' which is wrong
            elif alternatives[i + 1] == '-':
                if ('a' <= alt <= alternatives[i + 2] <= 'z' or
                    'A' <= alt <= alternatives[i + 2] <= 'Z' or
                   '0' <= alt <= alternatives[i + 2] <= '9'):
                    delta = 0
                    while delta <= (ord(alternatives[i + 2]) - ord(alt)):
                        selected.add(chr(ord(alt) + delta))
                        delta += 1
                    i += 3
                    continue
                else:
                    raise BadArgument()
        else:
            selected.add(alt)
            i += 1
    sub_queries = map(lambda x: '%s%s%s' % (query[:start],
                                            x.strip(), query[end + 1:]),
                      sorted(selected))
    for elt in sub_queries:
        for item in expand_bracket(elt):
            expanded.add(item)
    return expanded

This is all the available test cases:

def test_valid_bracket_expand_1(self):
        valid_bracket_pattern = 'a.[a-c]'
        expected = set(['a.a', 'a.b', 'a.c'])
        queries = controller_util.expand_bracket(valid_bracket_pattern)
        self.assertSetEqual(expected, queries)

        valid_bracket_pattern = 'a.[3-8]'
        expected = set(['a.3', 'a.4', 'a.5', 'a.6', 'a.7', 'a.8'])
        queries = controller_util.expand_bracket(valid_bracket_pattern)
        self.assertSetEqual(expected, queries)

        valid_bracket_pattern = 'a.[M-P]'
        expected = set(['a.M', 'a.N', 'a.O', 'a.P'])
        queries = controller_util.expand_bracket(valid_bracket_pattern)
        self.assertSetEqual(expected, queries)

        valid_bracket_pattern = 'a.[3-4].[a-c]'
        expected = set(['a.3.a', 'a.3.b', 'a.3.c', 'a.4.a', 'a.4.b', 'a.4.c'])
        queries = controller_util.expand_bracket(valid_bracket_pattern)
        self.assertSetEqual(expected, queries)

        valid_bracket_pattern = 'a.[3-4][a-c]'
        expected = set(['a.3a', 'a.3b', 'a.3c', 'a.4a', 'a.4b', 'a.4c'])
        queries = controller_util.expand_bracket(valid_bracket_pattern)
        self.assertSetEqual(expected, queries)

        valid_bracket_pattern = 'a.[3-4a-c]'
        expected = set(['a.3', 'a.4', 'a.a', 'a.b', 'a.c'])
        queries = controller_util.expand_bracket(valid_bracket_pattern)
        self.assertSetEqual(expected, queries)

        valid_bracket_pattern = 'a.[13-4a-c]'
        expected = set(['a.1', 'a.3', 'a.4', 'a.a', 'a.b', 'a.c'])
        queries = controller_util.expand_bracket(valid_bracket_pattern)
        self.assertSetEqual(expected, queries)

        valid_bracket_pattern = 'a.[13-4Ma-c]'
        expected = set(['a.1', 'a.3', 'a.4', 'a.M', 'a.a', 'a.b', 'a.c'])
        queries = controller_util.expand_bracket(valid_bracket_pattern)
        self.assertSetEqual(expected, queries)

        valid_bracket_pattern = 'a.[13-5-7]'
        expected = set(['a.1', 'a.3', 'a.4', 'a.5', 'a.-', 'a.7'])
        queries = controller_util.expand_bracket(valid_bracket_pattern)
        self.assertSetEqual(expected, queries)

        valid_bracket_pattern = 'a.[3cN_-]'
        expected = set(['a.3', 'a.c', 'a.N', 'a._', 'a.-'])
        queries = controller_util.expand_bracket(valid_bracket_pattern)
        self.assertSetEqual(expected, queries)

        # loss right bracket
        invalid_bracket_pattern = 'a.[-_bc]'
        expected = set(['a.-', 'a._', 'a.b', 'a.c'])
        queries = controller_util.expand_bracket(invalid_bracket_pattern)
        self.assertSetEqual(expected, queries)

        valid_bracket_pattern = 'a.[3cN-_]'
        with self.assertRaises(BadArgument):
            controller_util.expand_bracket(valid_bracket_pattern)

        invalid_bracket_pattern = 'a.[5-4]'
        with self.assertRaises(BadArgument):
            controller_util.expand_bracket(invalid_bracket_pattern)

        invalid_bracket_pattern = 'a.[c-a]'
        with self.assertRaises(BadArgument):
            controller_util.expand_bracket(invalid_bracket_pattern)

        invalid_bracket_pattern = 'a.[B-A]'
        with self.assertRaises(BadArgument):
            controller_util.expand_bracket(invalid_bracket_pattern)

        # invalid char in brackets
        invalid_bracket_pattern = 'a.[Bce[]'
        with self.assertRaises(BadArgument):
            controller_util.expand_bracket(invalid_bracket_pattern)

        # loss right bracket
        invalid_bracket_pattern = 'a.[Bce'
        with self.assertRaises(BadArgument):
            controller_util.expand_bracket(invalid_bracket_pattern)

        # loss right bracket
        invalid_bracket_pattern = 'a.[--_bc]'
        with self.assertRaises(BadArgument):
            controller_util.expand_bracket(invalid_bracket_pattern)

--------------------------------------DEPRECATED--------------------------------------------------

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment