What is the meaning of % in this python expression
Question: Can someone explain what this regular expression means? I am looking at
someone else's python code, and I just find myself curious as to what the
expression is doing. I am also not certain what the 2nd % sign means.

    
    
    regexStr = '(%s)' % '|'.join(['.*'.join(str(i) for i in p) for p in itertools.permutations(charList)])
    


Answer: So it does this:

    
    
    import itertools
    charList = [1, 2, 3]
    
    '(%s)' % '|'.join(['.*'.join(str(i) for i in p) for p in itertools.permutations(charList)])
    #>>> '(1.*2.*3|1.*3.*2|2.*1.*3|2.*3.*1|3.*1.*2|3.*2.*1)'
    

First it generates all of the permutations of the input (unique orders):

    
    
    for permutation in itertools.permutations(charList):
        print(permutation)
    #>>> (1, 2, 3)
    #>>> (1, 3, 2)
    #>>> (2, 1, 3)
    #>>> (2, 3, 1)
    #>>> (3, 1, 2)
    #>>> (3, 2, 1)
    

For each of these, it converts each item to a string and joins them with `.*`

    
    
    '.*'.join(str(i) for i in (1, 2, 3))
    #>>> '1.*2.*3'
    

Then it joins all of those with `|`

    
    
    '|'.join(['.*'.join(str(i) for i in p) for p in itertools.permutations(charList)])
    #>>> '1.*2.*3|1.*3.*2|2.*1.*3|2.*3.*1|3.*1.*2|3.*2.*1'
    

and finally uses `'(%s)' % result` to [wrap the result in
brackets](https://docs.python.org/3.4/tutorial/inputoutput.html#old-string-
formatting):

    
    
    '(%s)' % '|'.join(['.*'.join(str(i) for i in p) for p in itertools.permutations(charList)])
    #>>> '(1.*2.*3|1.*3.*2|2.*1.*3|2.*3.*1|3.*1.*2|3.*2.*1)'
    

The pattern `'1.*2.*3'` matches all sequences like `111111222333333`.

The patern `A|B|C|D` matches _one_ of `A`, `B`, `C` or `D`.

So the resulting regex matches any permutation, with each item repeated any
number of times (including 0).

The outer brackets make this a capturing group.

