Skip to content

Instantly share code, notes, and snippets.

@bashkirtsevich
Last active September 23, 2018 17:55
Show Gist options
  • Select an option

  • Save bashkirtsevich/71d53106a63afbf890066b05629c41c7 to your computer and use it in GitHub Desktop.

Select an option

Save bashkirtsevich/71d53106a63afbf890066b05629c41c7 to your computer and use it in GitHub Desktop.
RegEx word permute
from itertools import groupby
def make_regexp(word):
return "^{}[{}]+$".format(
"".join(
"(?!(.*{}){{{}}})".format(char, freq)
for char, freq in map(
lambda item: (item[0], sum(1 for _ in item[1]) + 1),
groupby(sorted(word))
)
),
"".join(set(word))
)
print(make_regexp("творец"))
sigleline
^(?!(.*а){4})(?!(.*г){2})(?!(.*ж){2})(?!(.*з){2})(?!(.*и){2})(?!(.*к){2})(?!(.*л){2})[зажигалка]+$
multiline
^(?!(.*а){4})
(?!(.*г){2})
(?!(.*ж){2})
(?!(.*з){2})
(?!(.*и){2})
(?!(.*к){2})
(?!(.*л){2})[зажигалка]+$
w='зажигалка'; echo $w | sed 's/./&\n/g;s/.$//' | sort | uniq -c | \
sed -r 's/^\s*([0-9]+)\s*(.)$/echo \\(?!\\(.*\2\\)\\{$((\1+1))\\}\\)/e;1s/^/^/;$s/$/['$w']+$/' | \
sed ':a;N;s/\n//;ta'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment