Reputation: 3448
I am using the following code for generating some passwords with symbols, numbers and digits.
Sometimes the code works and sometimes not, what is the problem?
import random
import string
import re
template="latex code... placeholder ...other latex code"
latex_passwords=''
for _ in range(30):
password=''.join(random.choice(string.ascii_lowercase+string.digits+string.ascii_uppercase+string.punctuation) for _ in range(12))
latex_passwords+=password+'\n'
template=re.sub(r'placeholder',latex_passwords,template)
Additional details: if you run the code 10 times, almost half of the times it will fail giving the following error at the line with re.sub()
File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/sre_parse.py", line 887, in addgroup
raise s.error("invalid group reference %d" % index, pos)
sre_constants.error: invalid group reference 8 at position 169
Upvotes: 1
Views: 124
Reputation: 225074
re.sub
interprets sequences of \number
in the replacement as references to groups in the pattern (among other escape sequences). You can use a plain string replacement instead:
template = template.replace('placeholder', latex_passwords)
Upvotes: 3