Skip to content

Instantly share code, notes, and snippets.

@beatcracker
Last active December 21, 2021 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beatcracker/a2c845ba791eac0d2ead3df89b7cdf5a to your computer and use it in GitHub Desktop.
Save beatcracker/a2c845ba791eac0d2ead3df89b7cdf5a to your computer and use it in GitHub Desktop.

Details

This StackOverflow answer shows how to generate mathematiclly proven regular expression to match a line that doesn't contain a word.

If you want to do the same, you need to run vcsn locally since vcsn sandbox doesn't work anymore.

How-to

  1. Start vcsn using Docker
docker run --rm -p 8888:8888 lrde/vcsn
  1. Open http://localhost:8888
  2. Create new notebook
  3. Insert new cell
  4. Add this code to the cell
import vcsn

# your string
input = 'nomatch'

# use proper charset
c = vcsn.context('lal_char(a-z), b')
e = c.expression('(%s){c}' % input)
a = e.automaton()

print(
    '^(' +
    str(
        a.expression()
    ).replace('+','|')  # + is usually denoted |
    .replace('\e','') # \e denotes the empty word
    .replace('[^]','.') # [^] is usually written .
    + ')$'
)
  1. Run notebook
  2. Simplify generated regex: http://ivanzuzak.info/noam/webapps/regex_simplifier/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment